-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Do not cache parametrized models when in the process of parametrizing another model #10704
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
Conversation
Deploying pydantic-docs with
|
Latest commit: |
6194229
|
Status: | ✅ Deploy successful! |
Preview URL: | https://24ecaaa2.pydantic-docs.pages.dev |
Branch Preview URL: | https://10279-2.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #10704 will not alter performanceComparing Summary
|
@Viicos, this should be unblocked now :) |
assert module.Foo.model_fields['bar'].annotation == typing.Optional[module.Bar[str]] | ||
assert module.Foo.model_fields['bar2'].annotation == typing.Union[int, module.Bar[float]] |
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.
These assertions are not valid anymore, as the Bar[str]
class on the annotation is no longer the same that the one we create by calling module.Bar[str]
here (as it isn't cached anymore).
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
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.
Nice work. Happy to see this move across the line 👍
Change Summary
Fixes #10279, partially fixes #9887.
We avoid caching parametrized models when in the process of parametrizing another model, as the result of
BaseModel.__class_getitem__
can returnPydanticRecursiveRef
instances in this case. In the model core schema generation logic, we mutateFieldInfo
instances if the annotation wasn't evaluated yet, meaning we could set the annotation to some typing construct containing aPydanticRecursiveRef
, e.g. with:Because these
FieldInfo
instances come from themodel_fields
attribute of the model class, having the caching logic here can result in a parametrized model class with aFieldInfo
containingPydanticRecursiveRef
instances when we later parametrize a model (e.g.Model1[str]
).Note that is hopefully not a long term solution, as this doesn't fix what is an even simpler repro: #9969.
Related issue number
Checklist