Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix metadata only #184

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ome_autogen/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
("Instrument", f"{MIXIN_MODULE}._instrument.InstrumentMixin", False),
("Reference", f"{MIXIN_MODULE}._reference.ReferenceMixin", True),
("BinData", f"{MIXIN_MODULE}._bin_data.BinDataMixin", True),
("Pixels", f"{MIXIN_MODULE}._pixels.PixelsMixin", True),
(
"StructuredAnnotations",
f"{MIXIN_MODULE}._structured_annotations.StructuredAnnotationsMixin",
Expand Down
19 changes: 19 additions & 0 deletions src/ome_types/_mixins/_pixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pydantic import root_validator

from ome_types._mixins._base_type import OMEType


class PixelsMixin(OMEType):
@root_validator(pre=True)
def _validate_root(cls, values: dict) -> dict:
if "metadata_only" in values:
if isinstance(values["metadata_only"], bool):
if not values["metadata_only"]:
values.pop("metadata_only")
else:
# type ignore in case the autogeneration hasn't been built
from ome_types.model import MetadataOnly # type: ignore

values["metadata_only"] = MetadataOnly()

return values
18 changes: 18 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,21 @@ def _sort_elements(element: ET.Element, recursive: bool = True) -> None:
# Recursively sort child elements for each subelement
for child in element:
_sort_elements(child)


@pytest.mark.parametrize("only", [True, False, {}, None])
def test_metadata_only(only: bool) -> None:
pix = model.Pixels(
metadata_only=only, # passing bool should be fine
size_c=1,
size_t=1,
size_x=1,
size_y=1,
size_z=1,
dimension_order="XYZCT",
type="uint8",
)
if only not in (False, None): # note that empty dict is "truthy" for metadata_only
assert pix.metadata_only
else:
assert not pix.metadata_only