-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
support for return_schema
on functional serializers
#5920
Conversation
Deploying with Cloudflare Pages
|
pydantic/functional_serializers.py
Outdated
@@ -18,13 +18,13 @@ class PlainSerializer: | |||
|
|||
Attributes: | |||
func (core_schema.SerializerFunction): The serializer function. | |||
json_return_type (core_schema.JsonReturnTypes | None, optional): The JSON return type. Defaults to `None`. | |||
return_schema: Schema to build serializer for return type. Defaults to `None`. |
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.
Would it make sense to change this to be more similar to @computed_field
? There, we have
return_type: Any = _UNSET
and docstring (which probably needs some updates):
return_type: optional return for serialization logic to expect when serialising to JSON, if included
this must be correct, otherwise a `TypeError` is raised.
If you don't include a return type Any is used, which does runtime introspection to handle arbitrary
objects.
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.
and logic:
if return_type is _UNSET:
# try to get it from the type annotation
res = _typing_extra.get_type_hints(_decorators.unwrap_wrapped_function(f), include_extras=True)
return_type = res.get('return', _UNSET)
if return_type is _UNSET:
raise PydanticUserError(
'Computed field is missing return type annotation or specifying `return_type`'
' to the `@computed_field` decorator (e.g. `@computed_field(return_type=int|str)`)',
code='model-field-missing-annotation',
)
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.
ye, probably good idea.
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.
And during schema generation we just do
return_type_schema = self.generate_schema(d.info.return_type)
in pydantic._internal._generate_schema.GenerateSchema._computed_field_schema
Companion to pydantic/pydantic-core#646.