Skip to content

Consolidate schema definitions logic in the _Definitions class #11208

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

Merged
merged 2 commits into from
Jan 7, 2025

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Jan 2, 2025

Some common patterns are defined as methods to avoid duplication (e.g. create_reference_to_schema, used to store a schema as a definition and returns a 'definition-reference' schema). A couple existing methods on the GenerateSchema class are moved (and renamed to better describe what they do) to the _Definitions class.

Proper docstrings are added to attribute and methods.

Type hints are tweaked when necessary.

This is meant to be a simplified changeset of #10887, only with the necessary changes present.

Change Summary

Related issue number

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

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jan 2, 2025
@Viicos Viicos force-pushed the definitions-improvements branch from fcd4818 to 5de851f Compare January 2, 2025 22:17
Copy link

cloudflare-workers-and-pages bot commented Jan 2, 2025

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: c388c19
Status: ✅  Deploy successful!
Preview URL: https://506c3228.pydantic-docs.pages.dev
Branch Preview URL: https://definitions-improvements.pydantic-docs.pages.dev

View logs

self.seen: set[str] = set()
self.definitions: dict[str, core_schema.CoreSchema] = {}
self._recursively_seen = set()
self._definitions = {}
Copy link
Member Author

Choose a reason for hiding this comment

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

@MarkusSintonen, in your PR, you made a distinction between definitions stored from create_reference_to_schema and definitions coming from unpack_definitions (in your PR, as _unpacked_definitions), i.e. definitions potentially coming from the cached attribute of Pydantic models.

What was the reason to do so?

Copy link

codspeed-hq bot commented Jan 2, 2025

CodSpeed Performance Report

Merging #11208 will not alter performance

Comparing definitions-improvements (c388c19) with main (da0449e)

Summary

✅ 46 untouched benchmarks

Comment on lines 827 to 829
# if schema['type'] == 'definition-ref':
# return core_schema.definition_reference_schema(schema_ref=schema['schema_ref'])
Copy link
Member Author

Choose a reason for hiding this comment

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

@MarkusSintonen, this is an addition in your PR. Seems like test passes without it. Do you remember why this might be required? I'm assuming this is part of #10655, where 'definition-ref' schemas are inlined in place, and thus this might some kind of safety copy?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm removing it from this PR, as if it needs to be included, this will be in the schema walking refactor PR.

Copy link
Contributor

github-actions bot commented Jan 2, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _generate_schema.py 2121-2123
  _schema_generation_shared.py
Project Total  

This report was generated by python-coverage-comment-action

@Viicos Viicos force-pushed the definitions-improvements branch from 5de851f to e3a19eb Compare January 7, 2025 14:59
@Viicos Viicos marked this pull request as ready for review January 7, 2025 14:59
@Viicos Viicos force-pushed the definitions-improvements branch from e3a19eb to b6e5c54 Compare January 7, 2025 15:01
Copy link
Contributor

@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.

I like this refactor overall, I think it does simplify things and make our already confusing defs/refs management logic a bit more clear.

Thanks for adding those docstrings as well.

Some common patterns are defined as methods to avoid duplication
(e.g. `create_reference_to_schema`, used to store a schema as a
definition and returns a `'definition-reference'` schema). A couple
existing methods on the `GenerateSchema` class are moved (and
renamed to better describe what they do) to the `_Definitions`
class.

Proper docstrings are added to attribute and methods.

Type hints are tweaked when necessary.
@Viicos Viicos force-pushed the definitions-improvements branch from b6e5c54 to 79bec86 Compare January 7, 2025 15:35
Copy link
Contributor

@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.

I'm happy with this given the re-namings, thanks!

@Viicos Viicos merged commit 796fed9 into main Jan 7, 2025
62 checks passed
@Viicos Viicos deleted the definitions-improvements branch January 7, 2025 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants