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

Add examples and json_schema_extra to @computed_field #8013

Merged
merged 3 commits into from Nov 6, 2023

Conversation

alexmojaki
Copy link
Contributor

@alexmojaki alexmojaki commented Nov 4, 2023

Change Summary

  • Adds two parameters to computed_field with the same meanings as in Field to add info to the JSON schema. examples solves the primary user request, json_schema_extra was also mentioned in the issue comments.
  • Updated some stale arg descriptions in the computed_field docstring,
  • Factored out common code for handling json_schema_extra in GenerateSchema.

Related issue number

Closes #7864

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @Kludex

repr: bool = True,
return_type: Any = PydanticUndefined,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved this so that the overload and the actual signature matched better so they were easier to compare. Then I noticed that here it's repr: bool = True but the actual signature has repr: bool | None = None, is that intentional?

Copy link
Member

Choose a reason for hiding this comment

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

Good question.

I don't think so. I think we should use repr: bool | None = None here as well. As far as I understand it, the reason we allow None here is so that we can enforce a default of True if the computed_field is public and False if it's not.

@alexmojaki alexmojaki changed the title Add examples to computed_field for JSON schema Add examples and json_schema_extra to @computed_field Nov 4, 2023
@alexmojaki alexmojaki marked this pull request as ready for review November 4, 2023 21:50
@alexmojaki
Copy link
Contributor Author

please review

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

LGTM otherwise. Thanks for factoring out the common logic re the get_json_schema_update_func and add_json_schema_extra. Definitely cleaner now.

pydantic/fields.py Show resolved Hide resolved
repr: bool = True,
return_type: Any = PydanticUndefined,
Copy link
Member

Choose a reason for hiding this comment

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

Good question.

I don't think so. I think we should use repr: bool | None = None here as well. As far as I understand it, the reason we allow None here is so that we can enforce a default of True if the computed_field is public and False if it's not.

@sydney-runkle
Copy link
Member

@alexmojaki,

I'm going to go ahead and merge this, thanks for your work on it. I opened an issue to add some better docs and type hints, as we've discussed in this PR.

@sydney-runkle sydney-runkle merged commit 5576936 into pydantic:main Nov 6, 2023
65 of 67 checks passed
@dmontagu dmontagu mentioned this pull request Feb 27, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pydantic computed field example values
3 participants