diff --git a/masque/label.py b/masque/label.py index 98a65df..d220fee 100644 --- a/masque/label.py +++ b/masque/label.py @@ -66,7 +66,7 @@ class Label(PositionableImpl, RepeatableImpl, AnnotatableImpl, Bounded, Pivotabl new._string = string new._offset = offset new._repetition = repetition - new._annotations = annotations if annotations is not None else {} + new._annotations = annotations return new def __copy__(self) -> Self: diff --git a/masque/ref.py b/masque/ref.py index 1d71432..0cc911f 100644 --- a/masque/ref.py +++ b/masque/ref.py @@ -103,7 +103,7 @@ class Ref( new._scale = scale new._mirrored = mirrored new._repetition = repetition - new._annotations = annotations if annotations is not None else {} + new._annotations = annotations return new def __copy__(self) -> 'Ref': diff --git a/masque/test/test_gdsii_arrow.py b/masque/test/test_gdsii_arrow.py index b005cd7..cec48c8 100644 --- a/masque/test/test_gdsii_arrow.py +++ b/masque/test/test_gdsii_arrow.py @@ -19,7 +19,7 @@ if not gdsii_arrow.is_available(): def _annotations_key(annotations: dict[str, list[object]] | None) -> tuple[tuple[str, tuple[object, ...]], ...] | None: - if annotations is None: + if not annotations: return None return tuple(sorted((key, tuple(values)) for key, values in annotations.items())) @@ -228,6 +228,25 @@ def test_raw_ref_grid_label_constructors_match_public() -> None: public_grid = Grid(a_vector=(20, 0), a_count=3, b_vector=(0, 30), b_count=2) assert raw_grid == public_grid + raw_ref_empty = Ref._from_raw( + offset=numpy.array([100, 200]), + rotation=numpy.pi / 2, + mirrored=False, + scale=1.0, + repetition=None, + annotations=None, + ) + public_ref_empty = Ref( + offset=(100, 200), + rotation=numpy.pi / 2, + mirrored=False, + scale=1.0, + repetition=None, + annotations=None, + ) + assert raw_ref_empty.annotations is None + assert raw_ref_empty == public_ref_empty + raw_ref = Ref._from_raw( offset=numpy.array([100, 200]), rotation=numpy.pi / 2, @@ -247,6 +266,19 @@ def test_raw_ref_grid_label_constructors_match_public() -> None: assert raw_ref == public_ref assert numpy.array_equal(raw_ref.as_transforms(), public_ref.as_transforms()) + raw_label_empty = Label._from_raw( + 'LEAF', + offset=numpy.array([3, 4]), + annotations=None, + ) + public_label_empty = Label( + 'LEAF', + offset=(3, 4), + annotations=None, + ) + assert raw_label_empty.annotations is None + assert raw_label_empty == public_label_empty + raw_label = Label._from_raw( 'LEAF', offset=numpy.array([3, 4]), diff --git a/masque/utils/comparisons.py b/masque/utils/comparisons.py index ffb7206..bb2dfee 100644 --- a/masque/utils/comparisons.py +++ b/masque/utils/comparisons.py @@ -9,7 +9,15 @@ def annotation2key(aaa: int | float | str) -> tuple[bool, Any]: return (isinstance(aaa, str), aaa) +def _normalized_annotations(annotations: annotations_t) -> annotations_t: + if not annotations: + return None + return annotations + + def annotations_lt(aa: annotations_t, bb: annotations_t) -> bool: + aa = _normalized_annotations(aa) + bb = _normalized_annotations(bb) if aa is None: return bb is not None elif bb is None: # noqa: RET505 @@ -36,6 +44,8 @@ def annotations_lt(aa: annotations_t, bb: annotations_t) -> bool: def annotations_eq(aa: annotations_t, bb: annotations_t) -> bool: + aa = _normalized_annotations(aa) + bb = _normalized_annotations(bb) if aa is None: return bb is None elif bb is None: # noqa: RET505