diff --git a/src/ome_types/_mixins/_base_type.py b/src/ome_types/_mixins/_base_type.py index b12d7e33..3bcbf70f 100644 --- a/src/ome_types/_mixins/_base_type.py +++ b/src/ome_types/_mixins/_base_type.py @@ -184,7 +184,7 @@ def _update_set_fields(self) -> None: Because pydantic isn't aware of mutations to sequences, it can't tell when a field has been "set" by mutating a sequence. This method updates the - self.__fields_set__ attribute to reflect that. We assume that if an attribute + `model_fields_set` attribute to reflect that. We assume that if an attribute is not None, and is not equal to the default value, then it has been set. """ update_set_fields(self) diff --git a/src/ome_types/_pydantic_compat.py b/src/ome_types/_pydantic_compat.py index 6f731df2..527aaca0 100644 --- a/src/ome_types/_pydantic_compat.py +++ b/src/ome_types/_pydantic_compat.py @@ -40,7 +40,7 @@ def update_set_fields(self: BaseModel) -> None: Because pydantic isn't aware of mutations to sequences, it can't tell when a field has been "set" by mutating a sequence. This method updates the - self.__fields_set__ attribute to reflect that. We assume that if an attribute + `model_fields_set` attribute to reflect that. We assume that if an attribute is not None, and is not equal to the default value, then it has been set. """ for field_name, field in self.model_fields.items(): diff --git a/src/ome_types/widget.py b/src/ome_types/widget.py index 297c4a57..af4d00e6 100644 --- a/src/ome_types/widget.py +++ b/src/ome_types/widget.py @@ -139,10 +139,7 @@ def update(self, ome: OME | str | None | dict) -> None: self._current_path = ome else: raise TypeError("must be OME object or string") - if hasattr(_ome, "model_dump"): - data = _ome.model_dump(exclude_unset=True) - else: - data = _ome.dict(exclude_unset=True) + data = _ome.model_dump(exclude_unset=True) self._fill_item(data) def _fill_item(self, obj: Any, item: QTreeWidgetItem = None) -> None: diff --git a/tests/test_names.py b/tests/test_names.py index 8fd33c65..04a6e753 100644 --- a/tests/test_names.py +++ b/tests/test_names.py @@ -1,19 +1,12 @@ from __future__ import annotations -import json from pathlib import Path -from typing import TYPE_CHECKING, Any import pytest -from pydantic import BaseModel, version import ome_types from ome_types import model -if TYPE_CHECKING: - from collections.abc import Sequence - -PYDANTIC2 = version.VERSION.startswith("2") TESTS = Path(__file__).parent KNOWN_CHANGES: dict[str, list[tuple[str, str | None]]] = { "OME.datasets": [ @@ -88,51 +81,6 @@ } -def _assert_names_match( - old: dict[str, Any], new: dict[str, Any], path: Sequence[str] = () -) -> None: - """Make sure every key in old is in new, or that it's in KNOWN_CHANGES.""" - for old_key, value in old.items(): - new_key = old_key - if old_key not in new: - _path = ".".join(path) - if _path in KNOWN_CHANGES: - for from_, new_key in KNOWN_CHANGES[_path]: # type: ignore - if old_key == from_ and (new_key in new or new_key is None): - break - else: - raise AssertionError( - f"Key {old_key!r} not in new model at {_path}: {list(new)}" - ) - else: - raise AssertionError(f"{_path!r} not in KNOWN_CHANGES") - - if isinstance(value, dict) and new_key in new: - _assert_names_match(value, new[new_key], (*path, old_key)) - - -def _get_fields(cls: type[BaseModel]) -> dict[str, Any]: - from pydantic.typing import display_as_type - - fields = {} - for name, field in cls.__fields__.items(): - if name.startswith("_"): - continue - if isinstance(field.type_, type) and issubclass(field.type_, BaseModel): - fields[name] = _get_fields(field.type_) - else: - fields[name] = display_as_type(field.outer_type_) # type: ignore - return fields - - -@pytest.mark.skipif(PYDANTIC2, reason="no need to check pydantic 2") -def test_names() -> None: - with (TESTS / "data" / "old_model.json").open() as f: - old_names = json.load(f) - new_names = _get_fields(ome_types.model.OME) - _assert_names_match(old_names, new_names, ("OME",)) - - V1_EXPORTS = [ ("affine_transform", "AffineTransform"), ("annotation", "Annotation"),