-
Notifications
You must be signed in to change notification settings - Fork 312
add polymorphic_serialization for models and dataclasses
#1881
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
base: main
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #1881 will not alter performanceComparing Summary
|
|
|
||
| @pytest.mark.parametrize('input_value', [ModelA(b'bite', 2.3456), SubclassA(b'bite', 2.3456)]) | ||
| def test_model_a(model_serializer: SchemaSerializer, input_value): | ||
| print(model_serializer, input_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| print(model_serializer, input_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
| fallback: A function to call when an unknown value is encountered, | ||
| if `None` a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised. | ||
| serialize_as_any: Whether to serialize fields with duck-typing serialization behavior. | ||
| polymorphic_serialization: Whether to override configured model and dataclass polymorphic serialization for this call. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should follow the existing pattern for the description? (That is, other parameters don't mention this "override" behavior of the configuration. Maybe in the actual docstring of the method, mention that these parameters override the corresponding configuration or similar).
| let type_: Bound<'_, PyString> = schema.get_as_req(intern!(py, "type"))?; | ||
| let type_ = type_.to_str()?; | ||
|
|
||
| if type_ == "model" || type_ == "dataclass" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if type_ == "model" || type_ == "dataclass" { | |
| // Note: it could make sense to generalize this behavior for any type that may have subclasses, | |
| // but apart from models and dataclasses, that would be for arbitrary types where custom serialization | |
| // has to be defined already. | |
| if type_ == "model" || type_ == "dataclass" { |
| print(inner_serializer) | ||
| print(outer_serializer) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| print(inner_serializer) | |
| print(outer_serializer) |
|
Thanks, I also added to the OP a note on an idea like |
Change Summary
Introduced as a possible solution to pydantic/pydantic#12382; the theory is that most uses of
serialize_as_anyare because users want to respect subtyping properly.This PR introduces a configuration option and runtime flag
polymorphic_serializationwhich is used to enable serializing subclasses of models and dataclasses as the subclass, not as the base class.To maintain backwards compatibility, the default is
False- models and dataclasses will be serialized as the exact type in the schema.When the config is set to
True, then models and dataclasess will be serialized as their runtime type. This also respects any model serializer the subclass may be using.Users can also pass
polymorphic_serializationas a runtime option to the serialization functions; doing so will override the config value (i.e. can be globally enabled or disabled).Not explored yet in this PR, an additional / alternative possibility could be to expose this as some kind of wrapper so that for foreign types one can use
Annotated[ForeignBaseModel, PolymorphicSerialization]to enable this even when the type didn't opt in.(An
Annotatedform may remove the need for the global runtime override.)Related issue number
pydantic/pydantic#12382
Checklist
pydantic-core(except for expected changes)