Backtestspydanticrun-20260923-170312#13465

Issue #13465 Merged fix PR # Run 23 Sep 2026 · run-20260923-170312 pydantic/pydantic

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

Worse

The merged fix was judged better than our patch.

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

Both patches are behaviorally equivalent and correctly fix the root cause: they reuse the parent's already-substituted PydanticExtraInfo when __pydantic_extra__ is only inherited, mirroring field inheritance. I traced the key scenarios — plain Sub(M[int]) (typevars_map empty → reuse parent's dict[str,int]), direct M[int] parametrization (typevars_map non-empty + complete → replace_types), local re-annotation (build from cls_annotations hint), and the 2-level Mid[int] chain — and both produce identical results. Their base-lookup idioms also converge on the same choice (A breaks on the first non-None base in bases order; B's reversed-with-overwrite lands on that same first non-None base). The tests are essentially identical, both fail before and pass after, and neither is superficial. The deciding difference is scope/efficiency, which ranks above craft. Patch A guards its base lookup with `'__pydantic_extra__' not in cls_annotations` and places the whole thing inside `if '__pydantic_extra__' in type_hints`, with an early `break` — so it does extra work only in the narrow inherited-extra case. Patch B places its `for base in reversed(bases)` loop unconditionally before the type_hints check AND omits a break, so it iterates every base's getattr on every single model class definition, including the majority of models that have no extra annotation at all. collect_model_fields runs at every model creation, so that is a real, if small, added cost on a hot path and unnecessary churn for the fix. Patch B is marginally more readable with its flat three-branch structure and per-scenario comments, but that craft edge does not outweigh A's tighter, better-scoped change given correctness and tests are equal.

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

12m 37s12 turns

Wall-clock time in the container

Full test suite

No new failures

6,422 passed · 368 skipped · 6,790 total

Contamination audit

Unknown

See the audit notes for this run

Patch size

57vs 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+49 −8
pydantic/_internal/_fields.pyindex 59c7f8d7..9ec308e9 100644@@ -421,16 +421,38 @@ def collect_model_fields(  # noqa: C901     if config_wrapper.use_attribute_docstrings:         _update_fields_from_docstrings(cls, fields) +    # Look up a `PydanticExtraInfo` provided by a base class (with type variables already+    # substituted), mirroring `parent_fields_lookup` for regular fields:+    parent_extra_info: PydanticExtraInfo | None = None+    for base in reversed(bases):+        if (base_extra_info := getattr(base, '__pydantic_extra_info__', None)) is not None:+            parent_extra_info = base_extra_info+     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,-        )+        if '__pydantic_extra__' in cls_annotations or parent_extra_info is None:+            # `__pydantic_extra__` is defined on the current class (not just inherited), or no+            # base provides a `PydanticExtraInfo`: build it from the type hint.+            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,+            )+        elif typevars_map and parent_extra_info.complete:+            # Parametrizing a generic model (e.g. `MyModel[int]`): substitute the type variables+            # in the parent's already-substituted annotation.+            pydantic_extra_info = PydanticExtraInfo(+                annotation=_generics.replace_types(parent_extra_info.annotation, typevars_map),+                complete=True,+            )+        else:+            # `__pydantic_extra__` is only inherited (e.g. a plain subclass of a parametrized+            # model): reuse the parent's already-substituted `PydanticExtraInfo`, mirroring how+            # regular fields are copied from the parent's `model_fields`.+            pydantic_extra_info = parent_extra_info      return fields, pydantic_extra_info, class_vars tests/test_main.pyindex f924cec9..f3154dec 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_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'} 
Merged fix · PR #+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'} 

What the agent reported

Review passes cleanly. The work is complete.

Summary

What was wrong

In collect_model_fields (pydantic/_internal/_fields.py), the __pydantic_extra__ handling always re-read the parent's raw annotation (dict[str, T]) from type_hints and called _generics.replace_types(ann, typevars_map). For a plain non-generic subclass of a parametrized model (class Sub(M[int])), typevars_map is empty, so the substitution was a no-op and the raw ~T survived — making extras on Sub silently unvalidated and dropping the extras type from the JSON schema. Regular fields avoided this by copying from the parent's already-substituted model_fields; the extra-info path never consulted the parent's substituted __pydantic_extra_info__.

What I changed

  • pydantic/_internal/_fields.py: Added a parent_extra_info lookup (mirroring the existing parent_fields_lookup) and split the extra-info build into three branches mirroring the regular-field inheritance logic:
  • defined-on-class / no parent info → build from the type hint (unchanged behavior);
  • elif typevars_map (direct parametrization, M[int]) → substitute type vars into the parent's already-substituted annotation;
  • inherited-only (Sub(M[int])) → reuse the parent's substituted PydanticExtraInfo.
  • tests/test_main.py: Added test_extra_generic_subclass, styled after the sibling test_extra_generic_class.

Test evidence

  • Regression test test_extra_generic_subclass failed before the fix (additionalProperties was {} instead of {'type': 'integer'}) and passes after.
  • test_extra_generic_class (direct-parametrization) still passes — no regression.
  • Broader suites tests/test_main.py tests/test_generics.py tests/test_forward_ref.py tests/test_edge_cases.py: 640 passed, 27 skipped, 9 xfailed — no new failures.
  • Reviewer confirmed the diff is minimal, correctly scoped, and the elif branch cannot regress direct parametrization.

Requirements disposition

  1. Sub(x='nope') should raise ValidationError — implemented; covered by pytest.raises(ValidationError) in the regression test.
  2. Sub JSON schema additionalProperties should be {'type': 'integer'} — implemented; asserted in the regression test (was the failing assertion pre-fix).
  3. Bring extra-info to parity with fields, keeping the same deferred-annotation limitation as #12000 — implemented; incomplete parent info falls through to the existing rebuild_model_fields deferral path (confirmed by reviewer), scoped to collect_model_fields as the report specified.
  4. 2-level generic chain (Mid[int]) already works — must not break — verified; full test_generics.py passes with no new failures.
  5. Regression test in the style of test_extra_generic_class — implemented; test_extra_generic_subclass, structurally identical to the sibling.

All changes are left uncommitted in the working tree; no scratch files remain.