Conversation
Deploying pydantic-docs with
|
| Latest commit: |
6a04e9f
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://ce767e46.pydantic-docs.pages.dev |
| Branch Preview URL: | https://dh-polymorphic-serialization.pydantic-docs.pages.dev |
Merging this PR will degrade performance by 8.43%
Performance Changes
Comparing |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
f062486 to
3f56f5d
Compare
polymorphic_serialization flagpolymorphic_serialization option
| !!! warning Polymorphic serialization of standard library dataclasses | ||
| Polymorphic serialization is only supported for Pydantic models and [Pydantic dataclasses](./dataclasses.md). | ||
| When using [standard library dataclasses][dataclasses], polymorphic serialization is *not* supported, | ||
| even if the dataclass is a subclass of a Pydantic dataclass. This may be fixed in a future Pydantic release. |
There was a problem hiding this comment.
How hard would it be to fallback to inference (as serialize_as_any does) in this case? I'm worried we recommend switching from serialize_as_any to polymorphic_serialization but then users discover that it's "less powerful" in this case.
I guess it's a trade-off between not having it supported for now (and being able to implement it properly later), and having the limited inference behavior now (and users could as such rely on it) but such behavior changing when we implement it properly (and so is a behavior change for users already relying on it).
d116784 to
eb7a7ec
Compare
6d6e402 to
ca84f7d
Compare
ca84f7d to
3917617
Compare
Change Summary
(Imported from pydantic/pydantic-core#1881)
Introduced as a possible solution to #12382; the theory is that most uses of serialize_as_any are because users want to respect subtyping properly.
This PR introduces a configuration option and runtime flag polymorphic_serialization which 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).Points of Discussion for this PR
AnnotatedmetadataNot 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[ForeignModel, PolymorphicSerialization]to enable this even when the type didn't opt in.(An
Annotatedform may remove the need for the global runtime override.)Non-Pydantic types
This method cannot really ever work for types which Pydantic doesn't know how to serialize, e.g. subclass of
intwill not be serialized polymorphically. If the subclass had a__pydantic_serializer__attribute it could in theory work, but I think that's the unlikely case.More complicated is stdlib dataclasses and
TypedDict. It might be reasonable to expect these to work, but I think that would require inspection of the subclass types at runtime and somehow caching a serializer for them. (i.e. a lot of runtime callback intoPydantic.)Backwards compatibility
For the concern listed above on non-pydantic types, one way to introduce support for them later would be to have the runtime flag accept a set of types to serialize, e.g.
polymorphic_serialization = {BaseModel, BaseDataClass, int}... but another way to avoid the problem entirely would be to not have the global runtime flag and use the
Annotatedform to apply this behavior only to supported types.Related issue number
#12382
Checklist