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

support for return_schema on functional serializers #5920

Merged
merged 3 commits into from
May 30, 2023

Conversation

samuelcolvin
Copy link
Member

@cloudflare-pages
Copy link

cloudflare-pages bot commented May 29, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e288232
Status: ✅  Deploy successful!
Preview URL: https://7d8de78b.pydantic-docs2.pages.dev
Branch Preview URL: https://support-return-serializer.pydantic-docs2.pages.dev

View logs

@@ -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`.
Copy link
Contributor

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.

Copy link
Contributor

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',
                )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ye, probably good idea.

Copy link
Contributor

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

@samuelcolvin samuelcolvin mentioned this pull request May 29, 2023
9 tasks
@samuelcolvin samuelcolvin merged commit c452a50 into main May 30, 2023
16 of 52 checks passed
@samuelcolvin samuelcolvin deleted the support-return-serializer branch May 30, 2023 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants