|
30 | 30 | log = logging.getLogger(__name__) |
31 | 31 |
|
32 | 32 | __all__ = ( |
33 | | - "model_alias", |
34 | 33 | "BaseModel", |
35 | 34 | "ModelRegistry", |
36 | 35 | "ModelType", |
@@ -118,19 +117,10 @@ def type_(self) -> PyObjectPath: |
118 | 117 | # where the default behavior is just to drop the mis-named value. This prevents that |
119 | 118 | extra="forbid", |
120 | 119 | ser_json_timedelta="float", |
| 120 | + # Polymorphic serialization is the behavior of allowing a subclass of a model (or Pydantic dataclass) to override serialization so that the subclass' serialization is used, rather than the original model types's serialization. This will expose all the data defined on the subclass in the serialized payload. |
| 121 | + polymorphic_serialization=True, |
121 | 122 | ) |
122 | 123 |
|
123 | | - # https://docs.pydantic.dev/latest/concepts/serialization/#overriding-the-serialize_as_any-default-false |
124 | | - def model_dump(self, **kwargs) -> dict[str, Any]: |
125 | | - if not kwargs.get("serialize_as_any"): |
126 | | - kwargs["serialize_as_any"] = True |
127 | | - return super().model_dump(**kwargs) |
128 | | - |
129 | | - def model_dump_json(self, **kwargs) -> str: |
130 | | - if not kwargs.get("serialize_as_any"): |
131 | | - kwargs["serialize_as_any"] = True |
132 | | - return super().model_dump_json(**kwargs) |
133 | | - |
134 | 124 | def __str__(self): |
135 | 125 | # Because the standard string representation does not include class name |
136 | 126 | return repr(self) |
@@ -200,7 +190,7 @@ def _base_model_validator(cls, v, handler, info): |
200 | 190 |
|
201 | 191 | if isinstance(v, PydanticBaseModel): |
202 | 192 | # Coerce from one BaseModel type to another (because it worked automatically in v1) |
203 | | - v = v.model_dump(serialize_as_any=True, exclude={"type_"}) |
| 193 | + v = v.model_dump(exclude={"type_"}) |
204 | 194 |
|
205 | 195 | return handler(v) |
206 | 196 |
|
@@ -272,20 +262,6 @@ def _is_config_subregistry(value): |
272 | 262 | return False |
273 | 263 |
|
274 | 264 |
|
275 | | -def model_alias(model_name: str) -> BaseModel: |
276 | | - """Function to alias a BaseModel by name in the root registry. |
277 | | -
|
278 | | - Useful for configs in hydra where we want a config object to point directly to another config object. |
279 | | -
|
280 | | - Args: |
281 | | - model_name: The name of the underlying model to point to in the registry |
282 | | - Example: |
283 | | - _target_: ccflow.model_alias |
284 | | - model_name: foo |
285 | | - """ |
286 | | - return BaseModel.model_validate(model_name) |
287 | | - |
288 | | - |
289 | 265 | ModelType = TypeVar("ModelType", bound=BaseModel) |
290 | 266 |
|
291 | 267 |
|
@@ -325,8 +301,7 @@ def _validate_name(cls, v): |
325 | 301 | @model_serializer(mode="wrap") |
326 | 302 | def _registry_serializer(self, handler): |
327 | 303 | values = handler(self) |
328 | | - models_serialized = {k: model.model_dump(serialize_as_any=True, by_alias=True) for k, model in self._models.items()} |
329 | | - values["models"] = models_serialized |
| 304 | + values["models"] = self._models |
330 | 305 | return values |
331 | 306 |
|
332 | 307 | @property |
|
0 commit comments