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 caches for collecting definitions and invalid schemas from a CoreSchema #7527
Conversation
Deploying with Cloudflare Pages
|
pydantic/_internal/_core_utils.py
Outdated
def collect_invalid_schemas(schema: core_schema.CoreSchema) -> list[core_schema.CoreSchema]: | ||
invalid_schemas: list[core_schema.CoreSchema] = [] | ||
child_invalid = False # track recursion | ||
|
||
def _is_schema_valid(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.CoreSchema: | ||
if s.get('metadata', {}).get('invalid'): | ||
nonlocal child_invalid | ||
metadata = s.setdefault('metadata', {}) | ||
invalid = metadata.get('invalid', None) | ||
if invalid is True: | ||
invalid_schemas.append(s) | ||
return recurse(s, _is_schema_valid) | ||
elif invalid is False: | ||
return s | ||
s = recurse(s, _is_schema_valid) | ||
if invalid is True: | ||
child_invalid = True | ||
metadata['invalid'] = child_invalid | ||
return s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on the motivation of this change? Maybe needs a comment, it's quite winding logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The invalid
thing already existed. What this is doing is marking a schema as "there are no invalid schemas in this subtree" (which is the most common case) so that we can skip recursing into it after the first time we recurse through it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But now that I think of it we should be able to do something similar to https://github.com/pydantic/pydantic/pull/7529/files#diff-2ebfa892ec1d09440eefd4b22f488ec38c1cde365394e5a4cea6edb484827ba5 and do this from within GenerateSchema
during our first recursion through the CoreSchema (we build it recursively).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll wait to make that change until #7529 is merged so I can re-use post_process_schema
and such
please review |
Selected Reviewer: @hramezani