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

DM-40366: Use default pydantic model for testing #878

Merged
merged 1 commit into from
Aug 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions python/lsst/daf/butler/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,26 @@
This will fail for data structures that have complex python classes
without a registered YAML representer.
"""
converted = False
if hasattr(inMemoryDataset, "model_dump") and hasattr(inMemoryDataset, "model_dump_json"):
# Pydantic-like model if both dump() and json() exist.
# Pydantic v2-like model if both model_dump() and model_json()
# exist.
with contextlib.suppress(Exception):
inMemoryDataset = inMemoryDataset.model_dump()
converted = True

if not converted and hasattr(inMemoryDataset, "dict") and hasattr(inMemoryDataset, "json"):
# Pydantic v1-like model if both dict() and json() exist.
with contextlib.suppress(Exception):
inMemoryDataset = inMemoryDataset.dict()
converted = True

Check warning on line 167 in python/lsst/daf/butler/formatters/yaml.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/formatters/yaml.py#L166-L167

Added lines #L166 - L167 were not covered by tests

if not converted:
if dataclasses.is_dataclass(inMemoryDataset):
inMemoryDataset = dataclasses.asdict(inMemoryDataset)
elif hasattr(inMemoryDataset, "_asdict"):
inMemoryDataset = inMemoryDataset._asdict()

if dataclasses.is_dataclass(inMemoryDataset):
inMemoryDataset = dataclasses.asdict(inMemoryDataset)
elif hasattr(inMemoryDataset, "_asdict"):
inMemoryDataset = inMemoryDataset._asdict()
unsafe_dump = self.writeParameters.get("unsafe_dump", False)
if unsafe_dump:
serialized = yaml.dump(inMemoryDataset)
Expand Down
11 changes: 8 additions & 3 deletions python/lsst/daf/butler/tests/_examplePythonTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from typing import TYPE_CHECKING, Any

from lsst.daf.butler import StorageClass, StorageClassDelegate
from lsst.daf.butler._compat import _BaseModelCompat
from pydantic import BaseModel

if TYPE_CHECKING:
from lsst.daf.butler import Butler, Datastore, FormatterFactory
Expand Down Expand Up @@ -264,7 +264,7 @@
return cls(exportDict["summary"], exportDict["output"], data)


class MetricsExampleModel(_BaseModelCompat):
class MetricsExampleModel(BaseModel):
"""A variant of `MetricsExample` based on model."""

summary: dict[str, Any] | None = None
Expand All @@ -274,7 +274,12 @@
@classmethod
def from_metrics(cls, metrics: MetricsExample) -> MetricsExampleModel:
"""Create a model based on an example."""
return cls.model_validate(metrics.exportAsDict())
d = metrics.exportAsDict()
# Assume pydantic v2 but fallback to v1
try:
return cls.model_validate(d)
except AttributeError:
return cls.parse_obj(d)

Check warning on line 282 in python/lsst/daf/butler/tests/_examplePythonTypes.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/tests/_examplePythonTypes.py#L281-L282

Added lines #L281 - L282 were not covered by tests


@dataclasses.dataclass
Expand Down