Skip to content
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

Computed fields don't work with TypedDict #657

Open
adriangb opened this issue Jun 3, 2023 · 4 comments · May be fixed by #1018
Open

Computed fields don't work with TypedDict #657

adriangb opened this issue Jun 3, 2023 · 4 comments · May be fixed by #1018
Labels
bug Something isn't working

Comments

@adriangb
Copy link
Member

adriangb commented Jun 3, 2023

They're in the schema but there are no tests and if you look at the source it's grabbing attributes, which won't work with TypedDict. We should do the same thing we do with model/field validators/serializer and hold a reference to the function before building the schema.

from pydantic_core import SchemaSerializer, core_schema

s = core_schema.typed_dict_schema({}, computed_fields=[core_schema.computed_field('foo', core_schema.int_schema())])
ser = SchemaSerializer(s)
ser.to_python({})  # AttributeError
@adriangb adriangb added the bug Something isn't working label Jun 3, 2023
@dmontagu
Copy link
Collaborator

dmontagu commented Jun 6, 2023

Might even make it a bit more performant too..

@adriangb
Copy link
Member Author

adriangb commented Jun 6, 2023

Yep! In any case, not an urgent issue since this is all new features.

@michaelhly
Copy link
Contributor

@adriangb given your example, what should foo be computed as?

@adriangb
Copy link
Member Author

I forget how we give it a function but ultimately there should be a function that gets called with the empty dict and comes up with a field value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants