Fix Sequence ignoring discriminator#9980
Merged
sydney-runkle merged 4 commits intopydantic:mainfrom Jul 29, 2024
Merged
Conversation
CodSpeed Performance ReportMerging #9980 will not alter performanceComparing Summary
|
Contributor
sydney-runkle
approved these changes
Jul 29, 2024
Contributor
sydney-runkle
left a comment
There was a problem hiding this comment.
Great job with this fix - discriminator fixes are often hard to track down!
This fix reflects an impressive understanding of the schema generation process!
8 tasks
Merged
5 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Change Summary
This is a fix for bug #9872. The annotation
Sequenceignoresdiscriminatormetadata in certain cases. The generated schema has not correctpython_schemawhere aunionis presented rather than expectedtagged-union.json_schemais normal.The bug occurs in
GenerateSchema._sequence_schema. In thejson-or-pythonschema created, the underling reference ofitem_type_schemais the same, therefore themetadatadict is also shared. When callingapply_discriminatorson the model schema, becausejson_schemais walked first, it has correct conversion fromuniontotagged-union. But the walk will pop the discriminator frommetadata. Therefore whenpython_schemais walked,metadatais already empty, resulting inunion, nottagged-unionschema.I suspect there are more of these kinds of reference sharing problems in other parts of schema generation process. 🤔
Note the author mentioned that the bug disappears when adding
model_validatorto both models. This is because the procedure for applying discriminators is different. It isGenerateSchema._apply_discriminator_to_unionthat generatestagged-unionin this case.Related issue number
fix #9872
Checklist