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_field: introspect return_type from annotation of wrapped method #7858

Closed
3 of 13 tasks
hottwaj opened this issue Oct 18, 2023 · 0 comments · Fixed by #7889
Closed
3 of 13 tasks

computed_field: introspect return_type from annotation of wrapped method #7858

hottwaj opened this issue Oct 18, 2023 · 0 comments · Fixed by #7889

Comments

@hottwaj
Copy link
Contributor

hottwaj commented Oct 18, 2023

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Hi there, the "computed_field" decorator provides a return_type kwarg which allows the user to specify the expected return type of the wrapped function e.g.

@computed_field(return_type=int)
def get_score(self, data: Any):
    return 2000

I have a use case where defining and subsequently extracting the return type of a computed field is useful (in order to make our use of pydantic models more generic).

It seems though that setting return_type could be done more naturally by using a type annotation on the wrapped function? e.g. like this

@computed_field
def get_score(self, data: Any) -> int:
    return 2000

i.e. it would be great if computed_field or another internal part of pydantic can, if no "return_type" kwarg is provided, introspect the wrapped function to extract the return type annotation and use that as "return_type"

Affected Components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant