Backtestspydanticrun-20260930-183947#13465

Issue #13465 Merged fix PR #13483 Run 30 Sep 2026 · run-20260930-183947 pydantic/pydantic

Generic pydantic_extra typevar substitution lost in non-generic subclasses of parametrized models

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause the same way. When `__pydantic_extra__` is only inherited, each reuses the parent's already-substituted `__pydantic_extra_info__` instead of the raw `dict[str, T]` hint, then applies `typevars_map` so the generic chain (`Mid[int]`) still works. Both resolve the reported `class Sub(M[int])` case.

Patch A is more careful about where the inherited annotation comes from. It walks the MRO and checks each class's own `__dict__` for `__pydantic_extra_info__`. It bails out (keeping the original behaviour) if the annotation actually comes from a non-Pydantic class such as a mixin, or if the parent's info is incomplete. This matches Python's annotation resolution order and keeps the deferred-annotation path unchanged.

Patch B uses `getattr` on direct `bases` only. That can pick up an inherited attribute that does not match true MRO precedence, for example when a non-Pydantic mixin that annotates `__pydantic_extra__` comes earlier in the MRO. B also forwards an incomplete parent info object as-is, which is a slightly different behaviour from before and relies on `rebuild_model_fields()`.

On tests, A covers more: `Sub`, `SubSub`, a multiple-inheritance case (`Other, Model[int]`), and a subclass that overrides the annotation. B tests only the basic `Sub` case, and its docstring is a leftover `<link>` placeholder.

On craft, A's small helper is clear and its diff is minimal. B restructures the block with more branching. Overall A is more robust and better tested.

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

9m 13s

Wall-clock time in the container

Full test suite

No new failures

12,269 passed · 501 skipped · 12,770 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

65vs 56 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2 (main @ 428b0dba8)
  • I have searched GitHub for a duplicate issue and I'm sure this is something new

Description

Follow-up to #13369 / #13391 (__pydantic_extra__ typevar substitution, currently main-only): the substitution works for direct parametrization (M[int]) but is lost in a plain (non-generic) subclass of a parametrized model (class Sub(M[int])). Extras on the subclass are silently unvalidated and the JSON schema loses the extras type.

M[int](x='nope')  -> ValidationError (correct, fixed by #13391)
Sub(x='nope')     -> accepted; m.x == 'nope'          # BUG
Sub(x='5').x      -> '5' (str)  vs  M[int](x='5').x == 5

M[int].model_json_schema()['additionalProperties']  -> {'type': 'integer'}
Sub.model_json_schema()['additionalProperties']     -> {}                  # BUG
Sub.__pydantic_extra_info__                          -> PydanticExtraInfo(annotation=dict[str, ~T], complete=True)

Root cause (from stepping through collect_model_fields in pydantic/_internal/_fields.py): for Sub, typevars_map is empty (its own __pydantic_generic_metadata__ is empty) and the inherited hint for __pydantic_extra__ is the parent's raw annotation dict[str, T] — so _generics.replace_types(ann, typevars_map) is a no-op and the raw ~T survives, producing extras_schema = {'type': 'any'}. Regular fields don't have this problem because they're copied from the parent's already-substituted model_fields; the extra-info path never consults the parent's __pydantic_extra_info__ (which on M[int] correctly holds dict[str, int]).

Note the 2-level generic chain (class Mid(M[T], Generic[T]); Mid[int]) works on main — only the plain-subclass case is broken.

I have a small fix working locally (reuse the parent's substituted PydanticExtraInfo annotation when __pydantic_extra__ is only inherited, mirroring how fields inherit; brings extra-info to exact parity with fields, including the same known deferred-annotation limitation as #12000), with a regression test in the style of test_extra_generic_class — it passes the test_main.py / test_generics.py / test_forward_ref.py / test_edge_cases.py suites with no new failures. Per the contributing guidelines I'd like to be assigned before opening the PR — happy to submit it right away.

Example Code

from typing import Generic, TypeVar
from pydantic import BaseModel, ConfigDict

T = TypeVar('T')

class M(BaseModel, Generic[T]):
    model_config = ConfigDict(extra='allow')
    __pydantic_extra__: dict[str, T]
    y: T

class Sub(M[int]):
    pass

print(Sub(y=1, x='not_an_int').x)  # 'not_an_int' — should raise ValidationError
print(Sub.model_json_schema().get('additionalProperties'))  # {} — should be {'type': 'integer'}

Python, Pydantic & OS Version

pydantic version: 2.14.0a1
pydantic-core version: 2.47.0
python version: 3.14.4
platform: macOS-26.5-arm64
commit: 428b0dba8 (main)

The two patches

Our patch+65 −0
pydantic/_internal/_fields.pyindex 59c7f8d7..e03dd6b2 100644@@ -424,6 +424,13 @@ def collect_model_fields(  # noqa: C901     pydantic_extra_info: PydanticExtraInfo | None = None     if '__pydantic_extra__' in type_hints:         ann, complete = type_hints['__pydantic_extra__']+        if (+            '__pydantic_extra__' not in cls_annotations+            and (parent_extra_info := _get_inherited_extra_info(cls)) is not None+        ):+            # Similar to fields, reuse the annotation from the parent model, as it might have had+            # type variables substituted (e.g. `class Sub(Model[int]): ...`):+            ann, complete = parent_extra_info.annotation, True         if complete:             # If not complete, `rebuild_model_fields()` takes care of it:             ann = _generics.replace_types(ann, typevars_map)@@ -435,6 +442,22 @@ def collect_model_fields(  # noqa: C901     return fields, pydantic_extra_info, class_vars  +def _get_inherited_extra_info(cls: type[BaseModel]) -> PydanticExtraInfo | None:+    """Get the (complete) `PydanticExtraInfo` instance of the closest parent model defining it.++    `None` is returned if the `__pydantic_extra__` annotation doesn't come from a Pydantic model,+    or if the parent's `PydanticExtraInfo` instance is incomplete.+    """+    for base in cls.__mro__[1:]:+        extra_info: PydanticExtraInfo | None = base.__dict__.get('__pydantic_extra_info__')+        if extra_info is not None:+            return extra_info if extra_info.complete else None+        if '__pydantic_extra__' in _typing_extra.safe_get_annotations(base):+            # The annotation comes from a class that isn't a Pydantic model (e.g. a mixin):+            return None+    return None++ def rebuild_model_fields(     cls: type[BaseModel],     *,tests/test_main.pyindex f924cec9..75e0911e 100644@@ -3507,6 +3507,48 @@ def test_extra_generic_class() -> None:     assert Mint(extra_value='1').model_extra == {'extra_value': 1}  +def test_extra_generic_class_non_generic_subclass() -> None:+    T = TypeVar('T')++    class Model(BaseModel, Generic[T], extra='allow'):+        __pydantic_extra__: dict[str, T]++    class Sub(Model[int]):+        pass++    class SubSub(Sub):+        pass++    class Other(BaseModel, extra='allow'):+        pass++    class MultipleBases(Other, Model[int]):+        pass++    for cls in (Sub, SubSub, MultipleBases):+        assert cls.model_json_schema()['additionalProperties'] == {'type': 'integer'}++        with pytest.raises(ValidationError):+            cls(extra_value='not_an_int')++        assert cls(extra_value='1').model_extra == {'extra_value': 1}+++def test_extra_generic_class_subclass_overrides_annotation() -> None:+    T = TypeVar('T')++    class Model(BaseModel, Generic[T], extra='allow'):+        __pydantic_extra__: dict[str, T]++    class Sub(Model[int]):+        __pydantic_extra__: dict[str, str]++    assert Sub.model_json_schema()['additionalProperties'] == {'type': 'string'}+    assert Sub(extra_value='a').model_extra == {'extra_value': 'a'}+    with pytest.raises(ValidationError):+        Sub(extra_value=1)++ def test_super_getattr_extra():     class Model(BaseModel):         model_config = {'extra': 'allow'} 
Merged fix · PR #13483+48 −8
pydantic/_internal/_fields.pyindex 59c7f8d79..af78e98db 100644@@ -423,14 +423,35 @@ def collect_model_fields(  # noqa: C901      pydantic_extra_info: PydanticExtraInfo | None = None     if '__pydantic_extra__' in type_hints:-        ann, complete = type_hints['__pydantic_extra__']-        if complete:-            # If not complete, `rebuild_model_fields()` takes care of it:-            ann = _generics.replace_types(ann, typevars_map)-        pydantic_extra_info = PydanticExtraInfo(-            annotation=ann,-            complete=complete,-        )+        parent_extra_info: PydanticExtraInfo | None = None+        if '__pydantic_extra__' not in cls_annotations:+            # The annotation is only present on parent classes. As with fields, we make use of the parent's+            # extra info, where type variables are already substituted:+            for base in bases:+                parent_extra_info = getattr(base, '__pydantic_extra_info__', None)+                if parent_extra_info is not None:+                    break++        if parent_extra_info is not None:+            # As with fields, the only case where substituting the type variables is relevant (i.e. when+            # `typevars_map` is not empty) is when a generic class is parameterized:+            if typevars_map and parent_extra_info.complete:+                # If not complete, `rebuild_model_fields()` takes care of it:+                pydantic_extra_info = PydanticExtraInfo(+                    annotation=_generics.replace_types(parent_extra_info.annotation, typevars_map),+                    complete=True,+                )+            else:+                pydantic_extra_info = parent_extra_info+        else:+            ann, complete = type_hints['__pydantic_extra__']+            if complete:+                # If not complete, `rebuild_model_fields()` takes care of it:+                ann = _generics.replace_types(ann, typevars_map)+            pydantic_extra_info = PydanticExtraInfo(+                annotation=ann,+                complete=complete,+            )      return fields, pydantic_extra_info, class_vars tests/test_main.pyindex f924cec99..b6511f5c2 100644@@ -3507,6 +3507,25 @@ def test_extra_generic_class() -> None:     assert Mint(extra_value='1').model_extra == {'extra_value': 1}  +def test_extra_generic_class_subclass() -> None:+    """https://github.com/pydantic/pydantic/issues/13465"""++    T = TypeVar('T')++    class Model(BaseModel, Generic[T], extra='allow'):+        __pydantic_extra__: dict[str, T]++    class Sub(Model[int]):+        pass++    assert Sub.model_json_schema()['additionalProperties'] == {'type': 'integer'}++    with pytest.raises(ValidationError):+        Sub(extra_value='not_an_int')++    assert Sub(extra_value='1').model_extra == {'extra_value': 1}++ def test_super_getattr_extra():     class Model(BaseModel):         model_config = {'extra': 'allow'}