-
Notifications
You must be signed in to change notification settings - Fork 232
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
feat: Computed field serialization for TypedDict #1018
Open
michaelhly
wants to merge
13
commits into
pydantic:main
Choose a base branch
from
michaelhly:computed-field-ser-func
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0929f62
feat: Serialize computed field without a model
michaelhly a1bba74
Check if model is a dict
michaelhly 4d830d7
clean up
michaelhly 264e6ac
Fix
michaelhly 747dd3e
Fallback behavior for ObType::Unknown
michaelhly d995188
Update tests
michaelhly fd5521c
Refactor get_next_value
michaelhly 6dc985f
Lint
michaelhly d293215
Address ser_schema comment
michaelhly 147e917
Add test to compute on TypedDict model
michaelhly bdc1326
Address comment on error message
michaelhly 144120f
Small clean up
michaelhly a4f0ca1
Merge remote-tracking branch 'origin/main' into computed-field-ser-func
michaelhly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
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.
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.
I don't think it's fair to call this "backwards compatibility" when this is still expected to be the main code path for models and dataclasses.
I wonder if there might be a more unified way. For a model or dataclass
A
with computed fieldb
, the analogous functionality really seems to beA.b.__get__(instance)
. For aTypedDict
it looks like that also works:So maybe what we really want, in all cases, is
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.
My thinking was that a serialization function should be provided to a computed field, doesn't matter if the input type is a
Model
,TypedDict
,Dataclass
, etc.The default behavior is to compute the computed value from the function provided in the serialization schema and then it gets set in the
output_dict
:pydantic-core/src/serializers/computed_fields.rs
Lines 144 to 154 in 866eb2d
This seems more generalizable to all computed fields instead of relying on the computed field defined as an attribute on the input value.
However, I am probably missing some context on how computed fields are used by https://github.com/pydantic/pydantic.
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.
I'm not following all that closely but my 2c is that ideally we extract the function from the thing in
pydantic
and not inpydantic-core
so that:__mro__
based on__orig_bases__
which we do for TypedDict)The con of that last one is that in theory someone could want us to use the method on a subclass they pass in as a value, which doesn't apply to TypedDict but also is not what we do for BaseModel and no one has complained 😄