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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unnecessary logic for definitions schema gen with discriminated unions #8951
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06a643b
add fix + test
sydney-runkle 324e386
typed dict test as well
sydney-runkle 05e2423
fix tests for 3.8 and 3.9
sydney-runkle 3fbbece
example of alternative approach
sydney-runkle 5fe1954
Merge branch 'main' into test-alternative-fix
sydney-runkle 0717cb4
fix failing test
sydney-runkle File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
from ._core_utils import ( | ||
CoreSchemaField, | ||
collect_definitions, | ||
simplify_schema_references, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
|
@@ -36,9 +35,10 @@ def set_discriminator_in_metadata(schema: CoreSchema, discriminator: Any) -> Non | |
|
||
|
||
def apply_discriminators(schema: core_schema.CoreSchema) -> core_schema.CoreSchema: | ||
# Throughout recursion, we allow references to be resolved from the definitions | ||
# that are present in the outermost schema. Before apply_discriminators is called, | ||
# we call simplify_schema_references (in the clean_schema function), | ||
# We recursively walk through the `schema` passed to `apply_discriminators`, applying discriminators | ||
# where necessary at each level. During this recursion, we allow references to be resolved from the definitions | ||
# that are originally present on the original, outermost `schema`. Before `apply_discriminators` is called, | ||
# `simplify_schema_references` is called on the schema (in the `clean_schema` function), | ||
# which often puts the definitions in the outermost schema. | ||
global_definitions: dict[str, CoreSchema] = collect_definitions(schema) | ||
|
||
|
@@ -55,7 +55,7 @@ def inner(s: core_schema.CoreSchema, recurse: _core_utils.Recurse) -> core_schem | |
s = apply_discriminator(s, discriminator, global_definitions) | ||
return s | ||
|
||
return simplify_schema_references(_core_utils.walk_core_schema(schema, inner)) | ||
return _core_utils.walk_core_schema(schema, inner) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woohoo! Performance benefit! |
||
|
||
|
||
def apply_discriminator( | ||
|
@@ -187,27 +187,11 @@ def apply(self, schema: core_schema.CoreSchema) -> core_schema.CoreSchema: | |
- If discriminator fields have different aliases. | ||
- If discriminator field not of type `Literal`. | ||
""" | ||
# Fetch the definitions attached to the (often inner) schema in question, | ||
# and add them to the definitions that we will use to resolve references | ||
original_local_defs = collect_definitions(schema) | ||
self.definitions.update(original_local_defs) | ||
|
||
assert not self._used | ||
schema = self._apply_to_root(schema) | ||
if self._should_be_nullable and not self._is_nullable: | ||
schema = core_schema.nullable_schema(schema) | ||
self._used = True | ||
|
||
# If there are any definitions that were present on the original schema but not on the new schema, | ||
# we need to add them to the new schema. This is necessary because the definitions may contain | ||
# schemas that are referenced by the choices in the union, and we need to ensure that the new schema | ||
# contains all the definitions that are necessary to resolve these references. | ||
# Note -- by "original schema", we refer to the schema that was passed to the apply method, | ||
# not the outermost schema that we're recursing on (where self.definitions came from). | ||
new_local_defs = collect_definitions(schema) | ||
missing_defs = original_local_defs.keys() - new_local_defs.keys() | ||
if missing_defs: | ||
schema = core_schema.definitions_schema(schema, [original_local_defs[ref] for ref in missing_defs]) | ||
return schema | ||
|
||
def _apply_to_root(self, schema: core_schema.CoreSchema) -> core_schema.CoreSchema: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just making this a bit more clear