Skip to content

Commit 1ac3bd8

Browse files
committed
Use polymorphic_serialization flag and small cleanup
Signed-off-by: Nijat Khanbabayev <[email protected]>
1 parent d6aa3ed commit 1ac3bd8

36 files changed

+1925
-243
lines changed

.copier-answers.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changes here will be overwritten by Copier
2-
_commit: f812aaa
2+
_commit: 973c39c
33
_src_path: https://github.com/python-project-templates/base.git
44
add_docs: false
55
add_extension: python
@@ -9,5 +9,5 @@ github: Point72
99
project_description: ccflow is a collection of tools for workflow configuration, orchestration,
1010
and dependency injection
1111
project_name: ccflow
12-
python_version_primary: '3.9'
12+
python_version_primary: '3.11'
1313
team: Point72, L.P.

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,11 @@ ccflow/labextension
157157

158158
# Rust
159159
target
160+
161+
# Examples
162+
outputs
163+
raw.html
164+
extracted.csv
165+
etl.db
166+
lobsters.html
167+
lobsters.csv

ccflow/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
__version__ = "0.6.8"
1+
__version__ = "0.7.0"
2+
3+
# Import exttypes early so modules that import `from ccflow import PyObjectPath` during
4+
# initialization find it (avoids circular import issues with functions that import utilities
5+
# which, in turn, import `ccflow`).
6+
from .exttypes import * # noqa: I001
27

38
from .arrow import *
49
from .base import *
10+
from .compose import *
511
from .callable import *
612
from .context import *
713
from .enums import Enum
8-
from .exttypes import *
914
from .global_state import *
1015
from .models import *
1116
from .object_config import *

ccflow/base.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
log = logging.getLogger(__name__)
3131

3232
__all__ = (
33-
"model_alias",
3433
"BaseModel",
3534
"ModelRegistry",
3635
"ModelType",
@@ -118,19 +117,10 @@ def type_(self) -> PyObjectPath:
118117
# where the default behavior is just to drop the mis-named value. This prevents that
119118
extra="forbid",
120119
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,
121122
)
122123

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-
134124
def __str__(self):
135125
# Because the standard string representation does not include class name
136126
return repr(self)
@@ -200,7 +190,7 @@ def _base_model_validator(cls, v, handler, info):
200190

201191
if isinstance(v, PydanticBaseModel):
202192
# 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_"})
204194

205195
return handler(v)
206196

@@ -272,20 +262,6 @@ def _is_config_subregistry(value):
272262
return False
273263

274264

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-
289265
ModelType = TypeVar("ModelType", bound=BaseModel)
290266

291267

@@ -325,8 +301,7 @@ def _validate_name(cls, v):
325301
@model_serializer(mode="wrap")
326302
def _registry_serializer(self, handler):
327303
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
330305
return values
331306

332307
@property

0 commit comments

Comments
 (0)