8000 Do not cache parametrized models when in the process of parametrizing… · pydantic/pydantic@712a257 · GitHub
[go: up one dir, main page]

Skip to content

Commit 712a257

Browse files
committed
Do not cache parametrized models when in the process of parametrizing another model
1 parent af32a86 commit 712a257

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pydantic/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,12 @@ def __class_getitem__(
804804

805805
submodel = _generics.create_generic_submodel(model_name, origin, args, params)
806806

807-
# Update cache
808-
_generics.set_cached_generic_type(cls, typevar_values, submodel, origin, args)
807+
# Cache the generated model *only* if not in the process of parametrizing
808+
# another model. In some valid scenarios, we miss the opportunity to cache
809+
# it but in some cases this results in `PydanticRecursiveRef` instances left
810+
# on `FieldInfo` annotations:
811+
if len(_generics.recursively_defined_type_refs()) == 1:
812+
_generics.set_cached_generic_type(cls, typevar_values, submodel, origin, args)
809813

810814
return submodel
811815

0 commit comments

Comments
 (0)
0