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

Pydantic computed field example values #7864

Closed
4 of 13 tasks
Tracked by #7928
rsaleev opened this issue Oct 18, 2023 · 6 comments · Fixed by #8013
Closed
4 of 13 tasks
Tracked by #7928

Pydantic computed field example values #7864

rsaleev opened this issue Oct 18, 2023 · 6 comments · Fixed by #8013
Assignees
Labels
feature request help wanted Pull Request welcome

Comments

@rsaleev
Copy link

rsaleev 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

Maybe my approach is not right but I use computed_field wrapper for injecting ORM values in root model and it will be for sure a cool feature to have examples like for Field, less code and integration with FastAPI/OpenAPI

Affected Components

@sydney-runkle
Copy link
Member

Hi @rsaleev,

Thanks for your feedback. Could you provide an example MRE code snippet so that we can better understand what you're asking for? Thanks 👍

@sydney-runkle sydney-runkle self-assigned this Oct 18, 2023
@sydney-runkle sydney-runkle added the awaiting author response awaiting response from issue opener label Oct 19, 2023
@rsaleev
Copy link
Author

rsaleev commented Oct 20, 2023

Hi @rsaleev,

Thanks for your feedback. Could you provide an example MRE code snippet so that we can better understand what you're asking for? Thanks 👍

computed_field(alias="marketPlaceAccountName", return_type=str)
 def marketplace_acccount_name(self):
        return self.marketplace.name

No examples available for OpenAPI, only attr will be shown with field type

 order_number: Optional[str] = Field(
        serialization_alias="orderNumber", examples=[None, "1234567"]
    )

Examples will be shown in rendered OpenAPI docs

@sydney-runkle sydney-runkle removed the awaiting author response awaiting response from issue opener label Oct 20, 2023
@sydney-runkle
Copy link
Member

@rsaleev,

Thanks for the response. This seems like a reasonable feature request - I think we could also include json_schema_extra and a few other attributes from Field for computed_field.

@sydney-runkle sydney-runkle added the help wanted Pull Request welcome label Oct 20, 2023
@TiansuYu
Copy link

Started looking into this issue without hard commitment (Also leave the comment as a marker, so I can find the page again).

@alexmojaki
Copy link
Contributor

PR: #8013

@adriangb
Copy link
Member

adriangb commented Nov 7, 2023

I see we merged #8013 but I do want to point out that Annotated and Field in the return type works:

from typing import Annotated

from pydantic import BaseModel, Field, computed_field


class Model(BaseModel):
    a: int
    b: int

    @computed_field
    @property
    def c(self) -> Annotated[int, Field(description='The sum of a and b', examples=[3, 5])]:
        return self.a + self.b

print(Model.model_json_schema(mode='serialization')['properties']['c'])
# {'description': 'The sum of a and b', 'examples': [3, 5], 'readOnly': True, 'title': 'C', 'type': 'integer'}

This avoids having to duplicate the entirety of Field into computed_field. Nonetheless I do think having the parameters in computed_field is more ergonomic for end users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants