Backtestspydanticrun-20261027-165153#11223
Type variables wrongly replaced when two models share the same type variable instance
Better
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The issue is that an unparameterized generic model (Model2) whose type variable T is shared with Model1 gets T substituted by Model1[int]'s typevars map. Patch B addresses this at the root: `replace_types` now returns an unparameterized generic model unchanged (treating it as implicitly parameterized with Any), `iter_contained_typevars` no longer reports parameters for unparameterized models, and `get_model_typevars_map` zips the origin's own parameters. Its added tests directly exercise the issue snippet (forward ref, direct reference, list container, Model2[T] still substituted) and all five independent tests fail on base and pass on B. Patch A instead changes which namespace/typevars map is used when rebuilding fields of a parametrized generic during schema generation. That fixes adjacent forward-ref problems (and un-xfails two tests), but it does not touch `replace_types`, so Model2 still has T replaced when Model1[int] is built; all five independent tests still raise ValidationError on A, and A's own nested-model test only covers a non-generic Inner, not the issue's shared-TypeVar scenario. Patch B does carry extra semantic churn (Model[T] is no longer `Model`, a new PydanticUndefinedAnnotation raise during self-referencing definition, updated existing test expectations), which adds some risk, but it is the only patch that resolves the reported behaviour.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
24m 55s
Wall-clock time in the container
Full test suite
No new failures
798 passed · 45 skipped · 843 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
197vs 233 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
From https://github.com/pydantic/pydantic/issues/9887#issuecomment-2382877486:
from typing import Generic, TypeVar
from pydantic import BaseModel
T = TypeVar('T')
class Model1(BaseModel, Generic[T]):
m2: 'Model2'
class Model2(BaseModel, Generic[T]):
t: T
Model1[int].model_validate({'m2': {'t': 'not_an_int'}})
# raises a ValidationError, shouldn't
Example Code
_No response_
Python, Pydantic & OS Version
2.10The two patches
Our patch+188 −9
pydantic/_internal/_generate_schema.pyindex 0d03cd29..2e890def 100644@@ -115,7 +115,7 @@ from ._mock_val_ser import MockCoreSchema from ._namespace_utils import NamespacesTuple, NsResolver from ._schema_gather import MissingDefinitionError, gather_schemas_for_cleaning from ._schema_generation_shared import CallbackGetCoreSchemaHandler-from ._utils import lenient_issubclass, smart_deepcopy+from ._utils import all_identical, lenient_issubclass, smart_deepcopy if TYPE_CHECKING: from ..fields import ComputedFieldInfo, FieldInfo@@ -1123,6 +1123,13 @@ class GenerateSchema: BaseModel = import_cached_base_model() if lenient_issubclass(obj, BaseModel):+ generic_origin = obj.__pydantic_generic_metadata__['origin'] if obj is not BaseModel else None+ if generic_origin is not None and all_identical(+ obj.__pydantic_generic_metadata__['args'], generic_origin.__pydantic_generic_metadata__['parameters']+ ):+ # `Model[T]` (where `T` is the type variable of `Model`) that wasn't substituted is equivalent+ # to the unparameterized `Model` (e.g. a self-referencing `children: list[Model[T]]` annotation):+ obj = generic_origin with self.model_type_stack.push(obj): return self._model_schema(obj) pydantic/_internal/_generics.pyindex 618b66ef..3e9ddece 100644@@ -198,7 +198,10 @@ def iter_contained_typevars(v: Any) -> Generator[TypeVar]: if isinstance(v, TypeVar): yield v elif is_model_class(v):- yield from v.__pydantic_generic_metadata__['parameters']+ # Unparameterized generic models (e.g. `Model` and not `Model[T]`) are implicitly+ # parameterized with `Any` and as such don't contain any type variable:+ if v.__pydantic_generic_metadata__['origin'] is not None:+ yield from v.__pydantic_generic_metadata__['parameters'] elif isinstance(v, (DictValues, list)): for var in v: yield from iter_contained_typevars(var)@@ -252,9 +255,8 @@ def get_model_typevars_map(cls: type[BaseModel]) -> dict[TypeVar, Any]: origin = generic_metadata['origin'] args = generic_metadata['args'] if not args:- # No need to go into `iter_contained_typevars`: return {}- return dict(zip(iter_contained_typevars(origin), args, strict=True))+ return dict(zip(origin.__pydantic_generic_metadata__['parameters'], args, strict=True)) def replace_types(type_: Any, type_map: Mapping[TypeVar, Any] | None) -> Any:@@ -333,6 +335,11 @@ def replace_types(type_: Any, type_map: Mapping[TypeVar, Any] | None) -> Any: # semantics as "typing" classes or generic aliases if not origin_type and is_model_class(type_):+ if type_.__pydantic_generic_metadata__['origin'] is None:+ # An unparameterized generic model (e.g. `Model` and not `Model[T]`) is treated+ # as being parameterized with `Any` (similar to how unparameterized classes are handled+ # by `typing`), even if its type variables are part of `type_map`:+ return type_ parameters = type_.__pydantic_generic_metadata__['parameters'] if not parameters: return type_pydantic/main.pyindex e980018e..be56acc1 100644@@ -989,9 +989,27 @@ class BaseModel(metaclass=_model_construction.ModelMetaclass): # We also update the provided args to use defaults values (`(str, bool)` becomes `(str, bool, int)`): typevar_values = tuple(v for v in typevars_map.values()) - if _utils.all_identical(typevars_map.keys(), typevars_map.values()) and typevars_map:- submodel = cls # if arguments are equal to parameters it's the same object+ # Note that unlike the unparameterized `Model` (implicitly parameterized with `Any`), `Model[T]` (where `T` is+ # the type variable of `Model`) is a new parameterized class, as the type variables should be substituted when used+ # in another generic model (e.g. with `class Outer[T](BaseModel): f: Model[T]`, `Outer[int]` should use `Model[int]`).+ if (+ typevars_map+ and _utils.all_identical(typevars_map.keys(), typevars_map.values())+ and cls.__pydantic_generic_metadata__['origin'] is not None+ ):+ # `cls` is already parameterized (e.g. `Model[int, T]`), so parameterizing it again+ # with its own type variables (e.g. `Model[int, T][T]`) gives back the same class:+ submodel = cls _generics.set_cached_generic_type(cls, typevar_values, submodel)+ elif (+ typevars_map+ and _utils.all_identical(typevars_map.keys(), typevars_map.values())+ and '__pydantic_fields__' not in cls.__dict__+ ):+ # This happens when the model is parameterized with its own type variables while being defined,+ # i.e. with a self-referencing annotation (e.g. `children: 'list[Model[T]]'`). As the parameterized+ # class can't be created yet, raise so that the annotation is evaluated once the model is defined:+ raise PydanticUndefinedAnnotation(cls.__name__, f'Class {cls.__name__!r} is not fully defined') else: parent_args = cls.__pydantic_generic_metadata__['args'] if not parent_args:tests/test_generics.pyindex 62b6f985..8416ae5e 100644@@ -1313,8 +1313,10 @@ def test_nested_identity_parameterization(): class Model(BaseModel, Generic[T]): a: T - assert Model[T][T][T] is Model- assert Model[T] is Model+ assert Model[T] is not Model+ assert Model[T].__pydantic_generic_metadata__ == {'origin': Model, 'args': (T,), 'parameters': (T,)}+ assert Model[T][T][T] is Model[T]+ assert Model[T][int] is Model[int] assert Model[T2] is not Model @@ -1444,7 +1446,8 @@ def test_replace_types_with_user_defined_generic_type_field(): # noqa: C901 tuple_field: CustomTuple[T] long_tuple_field: CustomLongTuple[T, VT] - assert replace_types(Model, {T: bool, KT: str, VT: int}) == Model[bool, str, int]+ # Unparameterized generic models are implicitly parameterized with `Any`:+ assert replace_types(Model, {T: bool, KT: str, VT: int}) is Model assert replace_types(Model[T, KT, VT], {T: bool, KT: str, VT: int}) == Model[bool, str, int] assert replace_types(Model[T, VT, KT], {T: bool, KT: str, VT: int}) == Model[T, VT, KT][bool, int, str] @@ -2160,6 +2163,150 @@ def test_generic_recursive_models_in_container(create_module): assert type(instance.foobar[0]) == MyGenericModel[int] +def test_generic_recursive_models_unparameterized_self_reference(create_module):+ @create_module+ def module():+ from typing import Generic, TypeVar++ from pydantic import BaseModel++ T = TypeVar('T')++ class MyGenericModel(BaseModel, Generic[T]):+ foobar: list['MyGenericModel'] | None+ spam: T++ MyGenericModel = module.MyGenericModel+ # The unparameterized `MyGenericModel` reference is implicitly parameterized with `Any`:+ instance = MyGenericModel[int](foobar=[{'foobar': [], 'spam': 'not_an_int'}], spam=1)+ assert type(instance.foobar[0]) is MyGenericModel+ assert instance.foobar[0].spam == 'not_an_int'+++def test_unparameterized_generic_model_sharing_type_variable_forward_ref() -> None:+ """https://github.com/pydantic/pydantic/issues/9887#issuecomment-2382877486"""++ T = TypeVar('T')++ class Model1(BaseModel, Generic[T]):+ m2: 'Model2'++ class Model2(BaseModel, Generic[T]):+ t: T++ m = Model1[int].model_validate({'m2': {'t': 'not_an_int'}})+ assert type(m.m2) is Model2+ assert m.m2.t == 'not_an_int'+ assert Model1[int].model_fields['m2'].annotation is Model2+++def test_unparameterized_generic_model_sharing_type_variable() -> None:+ T = TypeVar('T')++ class Model2(BaseModel, Generic[T]):+ t: T++ class Model1(BaseModel, Generic[T]):+ m2: Model2+ m2_list: list[Model2]+ m2_t: Model2[T]++ m = Model1[int].model_validate(+ {'m2': {'t': 'not_an_int'}, 'm2_list': [{'t': 'not_an_int'}], 'm2_t': {'t': '1'}},+ )+ assert type(m.m2) is Model2+ assert m.m2.t == 'not_an_int'+ assert type(m.m2_list[0]) is Model2+ assert m.m2_list[0].t == 'not_an_int'+ assert type(m.m2_t) is Model2[int]+ assert m.m2_t.t == 1++ with pytest.raises(ValidationError) as exc_info:+ Model1[int].model_validate({'m2': {'t': 1}, 'm2_list': [], 'm2_t': {'t': 'not_an_int'}})+ assert exc_info.value.errors(include_url=False) == [+ {+ 'type': 'int_parsing',+ 'loc': ('m2_t', 't'),+ 'msg': 'Input should be a valid integer, unable to parse string as an integer',+ 'input': 'not_an_int',+ }+ ]+++def test_unparameterized_generic_model_as_type_argument() -> None:+ T = TypeVar('T')++ class Model2(BaseModel, Generic[T]):+ t: T++ class Model1(BaseModel, Generic[T]):+ f: T++ # `Model2` is implicitly parameterized with `Any`, and as such doesn't make `Model1[Model2]` generic:+ assert Model1[Model2].__pydantic_generic_metadata__['parameters'] == ()+ assert Model1[Model2[T]].__pydantic_generic_metadata__['parameters'] == (T,)+ assert Model1[Model2[T]][int] is Model1[Model2[int]]+++def test_unsubstituted_self_parameterized_generic_model_is_origin() -> None:+ T = TypeVar('T')++ class Node(BaseModel, Generic[T]):+ value: T+ children: 'list[Node[T]]' = []++ class Inner(BaseModel, Generic[T]):+ x: T++ class Outer(BaseModel, Generic[T]):+ inner: Inner[T]++ # In the unparameterized models, `Node[T]` and `Inner[T]` are equivalent to `Node` and `Inner`:+ node = Node(value=1, children=[{'value': 'a'}])+ assert type(node.children[0]) is Node+ assert repr(node) == "Node(value=1, children=[Node(value='a', children=[])])"+ assert Node.model_json_schema() == {+ '$defs': {+ 'Node': {+ 'properties': {+ 'value': {'title': 'Value'},+ 'children': {+ 'default': [],+ 'items': {'$ref': '#/$defs/Node'},+ 'title': 'Children',+ 'type': 'array',+ },+ },+ 'required': ['value'],+ 'title': 'Node',+ 'type': 'object',+ }+ },+ '$ref': '#/$defs/Node',+ }++ inner = Inner(x=1)+ outer = Outer(inner=inner)+ assert outer.inner is inner+ assert Outer.model_json_schema()['$defs'].keys() == {'Inner'}++ # The type variables are still substituted in the parameterized models:+ assert type(Node[int](value=1, children=[{'value': '2'}]).children[0]) is Node[int]+ assert type(Outer[int](inner={'x': '1'}).inner) is Inner[int]+++def test_partially_parameterized_generic_model_reparameterized_with_own_type_variables() -> None:+ T = TypeVar('T')+ U = TypeVar('U')++ class Model(BaseModel, Generic[T, U]):+ a: T+ b: U++ assert Model[int, U][U] is Model[int, U]+ assert issubclass(Model[int, U][U], Model)++ def test_generic_enum(): T = TypeVar('T')
Merged fix · PR #14054+207 −26
pydantic/_internal/_fields.pyindex 51e7c9f01..a8ce945c1 100644@@ -603,13 +603,36 @@ def rebuild_model_fields( schema generation, where you don't want to mutate other model's fields. """ rebuilt_fields: dict[str, FieldInfo] = {}+ origin = cls.__pydantic_generic_metadata__['origin'] with ns_resolver.push(cls): for f_name, field_info in cls.__pydantic_fields__.items(): if field_info._complete: rebuilt_fields[f_name] = field_info else:+ annotation = field_info._original_annotation+ if origin is not None and (origin_field := origin.__pydantic_fields__.get(f_name)) is not None:+ # For parametrized generic models, the stored annotation can mix names from the origin class+ # and (forward) type arguments, which need to be evaluated in different namespaces. For instance:+ #+ # class Model[T](BaseModel):+ # a: dict[T, 'Later']+ #+ # M = Model['Item']+ #+ # The stored annotation of `M.a` is `dict['Item', 'Later']`, where `'Later'` must be evaluated+ # in the namespace of `Model` and `'Item'` in the namespace of `M`. Instead,+ # first evaluate the (unsubstituted) annotation of the origin in its own namespace:+ with ns_resolver.push(origin):+ annotation = _typing_extra.eval_type(+ origin_field._original_annotation,+ *ns_resolver.types_namespace,+ ) new_field = _recreate_field_info(- field_info, ns_resolver=ns_resolver, typevars_map=typevars_map, lenient=False+ field_info,+ ns_resolver=ns_resolver,+ typevars_map=typevars_map,+ lenient=False,+ annotation=annotation, ) update_field_from_config(config_wrapper, f_name, new_field) rebuilt_fields[f_name] = new_field@@ -641,9 +664,13 @@ def _recreate_field_info( typevars_map: Mapping[TypeVar, Any], *, lenient: bool,+ annotation: Any = PydanticUndefined, ) -> FieldInfo: FieldInfo_ = import_cached_field_info() + if annotation is PydanticUndefined:+ annotation = field_info._original_annotation+ existing_desc = field_info.description if lenient: ann = _generics.replace_types(field_info._original_annotation, typevars_map)@@ -655,7 +682,7 @@ def _recreate_field_info( # Not the best pattern, maybe we could ship our own `eval_type()`, # that would replace the type variables on the fly during evaluation. ann = _typing_extra.eval_type(- field_info._original_annotation,+ annotation, *ns_resolver.types_namespace, ) ann = _generics.replace_types(ann, typevars_map)pydantic/_internal/_generate_schema.pyindex 0d03cd299..866db4e9a 100644@@ -109,7 +109,7 @@ from ._fields import ( update_field_from_config, ) from ._forward_ref import PydanticRecursiveRef-from ._generics import get_standard_typevars_map, replace_types+from ._generics import get_model_typevars_map, get_standard_typevars_map, replace_types from ._import_utils import import_cached_base_model, import_cached_field_info from ._mock_val_ser import MockCoreSchema from ._namespace_utils import NamespacesTuple, NsResolver@@ -884,7 +884,7 @@ class GenerateSchema: cls, config_wrapper=self._config_wrapper, ns_resolver=self._ns_resolver,- typevars_map=self._typevars_map or {},+ typevars_map=get_model_typevars_map(cls), ) except NameError as e: raise PydanticUndefinedAnnotation.from_name_error(e) from etests/test_forward_ref.pyindex 80af7b36b..66a3f0fe9 100644@@ -1174,11 +1174,6 @@ class Foo(Bar): assert module_2.Foo.model_fields['f'].annotation is int -@pytest.mark.xfail(- reason='Same as `test_uses_the_correct_globals_to_resolve_model_forward_refs()`. As the generic model is '- 'parametrized in `module_2`, the `__module__` of the parametrized class is `module_2` and so `f` is '- 'evaluated in this module when rebuilding.'-) def test_uses_the_correct_globals_to_resolve_parametrized_generic_model_forward_refs(create_module): @create_module def module_1():@@ -1741,32 +1736,25 @@ def test_parameterized_with_annotated_forward_refs() -> None: assert M.model_fields['c'].metadata == [Gt(2), 3, 2] -@pytest.mark.xfail(- reason=(- 'Similar to `test_uses_the_correct_globals_to_resolve_model_forward_refs()`,'- "the NsResolver used for the `M.model_rebuild()` call doesn't make use of `Parent`, "- "so its `__type_params__` aren't available (they contain `T`)."- )-) @pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.') def test_parameterized_pep695_generic_with_annotated_forward_refs(create_module) -> None: mod = create_module( """- from typing import Annotated+from typing import Annotated - from pydantic import BaseModel+from pydantic import BaseModel, Field - class Parent[T](BaseModel):- a: T- b: 'MyAnnotated[T, 1]'- c: Annotated[T, 2] = Field(gt=2)+class Parent[T](BaseModel):+ a: T+ b: 'MyAnnotated[T, 1]'+ c: Annotated[T, 2] = Field(gt=2) - M = Parent[Annotated['MyInt', 3]]+M = Parent[Annotated['MyInt', 3]] - MyAnnotated = Annotated- MyInt = int+MyAnnotated = Annotated+MyInt = int - M.model_rebuild()+M.model_rebuild() """ ) @@ -1783,6 +1771,172 @@ def test_parameterized_pep695_generic_with_annotated_forward_refs(create_module) assert M.model_fields['c'].metadata == [Gt(2), 3, 2] +@pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.')+def test_pep695_mutually_recursive_generic_models(create_module) -> None:+ """https://github.com/pydantic/pydantic/issues/14021"""++ mod = create_module(+ """+from pydantic import BaseModel++class Node[T1](BaseModel):+ val: T1+ parent: 'Parent[T1] | None' = None++class Parent[T2](BaseModel):+ child: 'Node[T2]'+ """+ )++ # When `Parent` is created, `Node[T2]` is created while `Parent` isn't defined yet. The `parent` field+ # of `Node[T2]` is then rebuilt during `Parent`'s schema generation, where `T1` must be resolved from+ # the origin class (`Node`):+ mod.Parent.model_rebuild()++ assert mod.Parent(child={'val': 1}).child.val == 1+++@pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.')+def test_parametrized_generic_model_uses_origin_namespace(create_module) -> None:+ mod = create_module(+ """+from pydantic import BaseModel++class Model[T](BaseModel):+ class Inner(BaseModel):+ v: int++ a: 'Inner | Later'+ b: 'list[T]'++M = Model[int]++class Later(BaseModel):+ pass++M.model_rebuild()+ """+ )++ M = mod.M++ assert M.model_fields['a'].annotation == mod.Model.Inner | mod.Later+ assert M.model_fields['b'].annotation == list[int]+++def test_parametrized_generic_model_rebuilt_during_schema_generation() -> None:+ T = TypeVar('T')++ class Model(BaseModel, Generic[T]):+ x: 'Later[T]'++ # `x` can't be evaluated yet, and will be when building the schema of `Main`:+ ModelInt = Model[int]++ class Later(BaseModel, Generic[T]):+ y: T++ class Main(BaseModel):+ m: ModelInt++ # The type variables map of `Model[int]` (and not `Main`'s) must be used:+ with pytest.raises(ValidationError):+ Main(m={'x': {'y': 'not_an_int'}})+++@pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.')+def test_parametrized_generic_model_forward_type_arg_named_like_type_param(create_module) -> None:+ mod = create_module(+ """+from pydantic import BaseModel++class Page[Item](BaseModel):+ items: list[Item]++PageItem = Page['Item']++class Item(BaseModel):+ id: int++PageItem.model_rebuild()+ """+ )++ # `'Item'` refers to the `Item` class, not to the `Item` type parameter of `Page`:+ assert mod.PageItem.model_fields['items'].annotation == list[mod.Item]++ with pytest.raises(ValidationError):+ mod.PageItem(items=['not_an_item'])+++@pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.')+def test_parametrized_generic_model_forward_type_arg_named_like_nested_class(create_module) -> None:+ mod = create_module(+ """+from pydantic import BaseModel++class Wrapper[T](BaseModel):+ class Meta(BaseModel):+ inner: int++ value: T++WrapperMeta = Wrapper['Meta']++class Meta(BaseModel):+ outer: int++WrapperMeta.model_rebuild()+ """+ )++ # `'Meta'` refers to the module-level `Meta` class, not to `Wrapper.Meta`:+ assert mod.WrapperMeta.model_fields['value'].annotation is mod.Meta+++@pytest.mark.skipif(sys.version_info < (3, 12), reason='Test related to PEP 695 syntax.')+def test_parametrized_generic_model_forward_type_arg_named_like_class_var(create_module) -> None:+ mod = create_module(+ """+from typing import ClassVar++from pydantic import BaseModel++class Wrapper[T](BaseModel):+ Alias: ClassVar[str] = 'some value'++ value: T++WrapperAlias = Wrapper['Alias']++Alias = int++WrapperAlias.model_rebuild()+ """+ )++ # `'Alias'` refers to the module-level `Alias` type alias, not to `Wrapper.Alias`:+ assert mod.WrapperAlias.model_fields['value'].annotation is int+++def test_nested_model_type_variable_not_substituted_by_parent_typevars_map() -> None:+ """https://github.com/pydantic/pydantic/issues/11223"""++ T = TypeVar('T')++ class Inner(BaseModel):+ x: 'Later[T]'++ class Outer(BaseModel, Generic[T]):+ inner: Inner++ class Later(BaseModel, Generic[T]):+ y: T++ # `Inner` isn't generic, and shouldn't have `T` substituted by the type variables map of `Outer[int]`+ assert Outer[int](inner={'x': {'y': 'not_an_int'}}).inner.x.y == 'not_an_int'++ def test_string_annotation_union_type() -> None: """https://github.com/pydantic/pydantic/issues/12732""" T = TypeVar('T')