Backtestspydanticrun-20260930-144322#13465
Generic pydantic_extra typevar substitution lost in non-generic subclasses of parametrized models
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause the issue identifies. When `__pydantic_extra__` is only inherited, each uses the parent's already-substituted `__pydantic_extra_info__` instead of the raw `dict[str, T]` hint. Both effectively give priority to the first base: A breaks on the first match, and B's reversed loop ends with bases[0]. For the reported case (`class Sub(M[int])`) and for deeper plain subclasses, both produce `dict[str, int]` and validate correctly. The main difference is how completeness is tracked. Patch A takes the parent's `PydanticExtraInfo` as a whole, annotation and `complete` flag together. It only substitutes when `typevars_map` is non-empty, which mirrors how fields are inherited, as the issue describes. Patch B takes `complete` from the subclass's own type-hint evaluation but the annotation from the parent. The two can disagree. If the parent is complete but the subclass's hint evaluates as incomplete, B marks the info incomplete. The later rebuild would then re-derive the annotation from the raw hints, which can bring back the unsubstituted `~T`. In the opposite case, an incomplete parent, B falls back to the raw hint. This inconsistency is small but real; A keeps the two values coherent. On tests, both add a regression test that fails before the change and passes after. B's test is slightly stronger because it also covers a `SubSub(Sub)` chain. A's code is a bit more verbose, but it is explicit and follows the field-inheritance convention. Weighing correctness first, A's cleaner handling of completeness outweighs B's slightly broader test.
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
3m 7s9 turns
Wall-clock time in the container
Full test suite
No new failures
12,268 passed · 501 skipped · 12,769 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
36vs 56 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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
pydantic/_internal/_fields.pyindex 59c7f8d7..ff7d817a 100644@@ -424,6 +424,15 @@ 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:+ # The annotation is inherited: as for fields, use the parent's (already+ # parametrized) annotation, as the type hint may contain unsubstituted type variables:+ 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+ if parent_extra_info is not None and parent_extra_info.complete:+ ann = parent_extra_info.annotation if complete: # If not complete, `rebuild_model_fields()` takes care of it: ann = _generics.replace_types(ann, typevars_map)tests/test_main.pyindex f924cec9..c6415e8d 100644@@ -3507,6 +3507,33 @@ 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:+ """Follow-up to https://github.com/pydantic/pydantic/issues/13369: non-generic subclass of a parametrized model."""++ 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}++ class SubSub(Sub):+ pass++ with pytest.raises(ValidationError):+ SubSub(extra_value='not_an_int')++ assert SubSub(extra_value='1').model_extra == {'extra_value': 1}++ def test_super_getattr_extra(): class Model(BaseModel): model_config = {'extra': 'allow'}
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
I fixed the bug: a plain subclass of a parametrized model (class Sub(M[int])) now validates extras as int and puts {'type': 'integer'} back in the JSON schema. The new test and the adjacent suites pass. Nothing is committed.
What was wrong: In collect_model_fields (pydantic/_internal/_fields.py), Sub has no type-variable mapping of its own, so substitution did nothing. The __pydantic_extra__ hint it inherits is the parent's raw dict[str, T], so T stayed unresolved and extras were accepted as Any. Regular fields don't hit this because they are copied from the parent's already-substituted __pydantic_fields__.
What I changed (pydantic/_internal/_fields.py, +9 lines): if the class only inherits __pydantic_extra__ and doesn't annotate it itself, it now reuses the parent's already-substituted annotation from __pydantic_extra_info__. The parent is chosen the same way inherited fields are: the first base wins. Type-variable substitution still runs afterwards, so M[int] keeps working. If the parent's info is missing or not yet complete, behaviour is unchanged.
Test evidence:
- New
test_extra_generic_class_non_generic_subclassintests/test_main.py, placed aftertest_extra_generic_class. It checksSub's schema ({'type': 'integer'}), thatValidationErroris raised for a non-int extra, and thatmodel_extra == {'extra_value': 1}. It checks the error and the conversion for a further subclassSubSub(Sub)too. - Before the fix it failed with
assert {} == {'type': 'integer'}; after the fix it passes. - Gate:
./bt-test tests/test_main.py tests/test_generics.py tests/test_forward_ref.py tests/test_edge_cases.py tests/test_json_schema.pygave 1171 passed, 30 skipped, 10 xfailed, 0 failed.
Requirements:
Sub(x='nope')must raiseValidationError: done, covered by the new test.Sub(x='5').xmust be5: done. The test checks this throughmodel_extra, and a manual script printed5.Sub.model_json_schema()['additionalProperties']must be{'type': 'integer'}: done, in the new test.Sub.__pydantic_extra_info__must holddict[str, int]: done. The manual script printedPydanticExtraInfo(annotation=dict[str, int], complete=True); the test doesn't assert this directly.- Direct
M[int]must keep working: done, the existingtest_extra_generic_classstill passes. - The two-level chain
Mid[int]must keep working: done, the manual script printed{'type': 'integer'}. No test covers it. - Match how fields inherit, including the known deferred-annotation limitation (#12000): done. The reviewer confirmed the base precedence and the reuse of already-substituted parent info.
Edge cases the reviewer raised, which I left alone to keep the change small:
- Diamond inheritance: with
class C(A, B)where onlyBoverrides the annotation,Cnow uses the annotation viaA, notB's override. Fields already behave this way, but for extras it is a change from before. - Forward references: if the parent's extras annotation hadn't resolved yet (a forward reference), the plain-subclass case can still lose the substitution. This is the #12000 limitation the report accepted.
completeflag: it still comes from the subclass's own hint. At worst this delays building the schema until a rebuild.