This repository was archived by the owner on Apr 11, 2026. It is now read-only.
add polymorphic_serialization for models and dataclasses#1881
Closed
davidhewitt wants to merge 6 commits intomainfrom
Closed
add polymorphic_serialization for models and dataclasses#1881davidhewitt wants to merge 6 commits intomainfrom
polymorphic_serialization for models and dataclasses#1881davidhewitt wants to merge 6 commits intomainfrom
Conversation
CodSpeed Performance ReportMerging #1881 will not alter performanceComparing Summary
|
davidhewitt
commented
Nov 2, 2025
|
|
||
| @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) |
Collaborator
Author
There was a problem hiding this comment.
Suggested change
| print(model_serializer, input_value) |
Viicos
reviewed
Nov 3, 2025
| 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. |
Member
There was a problem hiding this comment.
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" { |
Member
There was a problem hiding this comment.
Suggested change
| 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" { |
Comment on lines
+102
to
+104
| print(inner_serializer) | ||
| print(outer_serializer) | ||
|
|
Member
There was a problem hiding this comment.
Suggested change
| print(inner_serializer) | |
| print(outer_serializer) |
Collaborator
Author
|
Thanks, I also added to the OP a note on an idea like |
5 tasks
Collaborator
Author
|
Moved to pydantic/pydantic#12518 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)