Skip to content

Commit

Permalink
fix refcopuy
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Jan 2, 2025
1 parent 13e4d39 commit b43a8cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ome_types/_mixins/_ome.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

if TYPE_CHECKING:
from pathlib import Path
from typing import Self

from ome_types._autogenerated.ome_2016_06 import OME, Reference

Expand All @@ -31,6 +32,11 @@ def _link_refs(self) -> None:
else:
warnings.warn(f"Reference to unknown ID: {ref.id}", stacklevel=2)

def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self:
copy = super().__deepcopy__(memo) # type: ignore
copy._link_refs()
return copy

def __setstate__(self, state: dict[str, Any]) -> None:
"""Support unpickle of our weakref references."""
super().__setstate__(state) # type: ignore
Expand Down
23 changes: 23 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import datetime
import io
import warnings
Expand All @@ -10,6 +11,7 @@
from pydantic import ValidationError

from ome_types import from_tiff, from_xml, model, to_xml
from ome_types.model import OME, AnnotationRef, CommentAnnotation, Instrument

DATA = Path(__file__).parent / "data"

Expand Down Expand Up @@ -42,6 +44,27 @@ def test_refs() -> None:
assert ome.screens[0].plate_refs[0].ref is ome.plates[0]


def test_ref_copy() -> None:
aref = AnnotationRef(id=1)
ome = OME(
instruments=[Instrument(annotation_refs=[aref])],
structured_annotations=[CommentAnnotation(id=1, value="test")],
)

ome2 = ome.model_copy(deep=True)
assert ome2.instruments[0].annotation_refs[0].ref is not aref.ref

ome3 = copy.deepcopy(ome)
assert ome3.instruments[0].annotation_refs[0].ref is not aref.ref
ome4 = OME(**ome.dict())
assert ome4.instruments[0].annotation_refs[0].ref is not aref.ref

del ome, aref
assert ome2.instruments[0].annotation_refs[0].ref is not None
assert ome3.instruments[0].annotation_refs[0].ref is not None
assert ome4.instruments[0].annotation_refs[0].ref is not None


def test_datetimes() -> None:
now = datetime.datetime.now()
anno = model.TimestampAnnotation(value=now)
Expand Down

0 comments on commit b43a8cd

Please sign in to comment.