Backtestspydanticrun-20260929-204949#13465

Issue #13465 Merged fix PR # Run 29 Sep 2026 · run-20260929-204949 pydantic/pydantic

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

Error

The harness couldn't finish this ticket.

api-error at solve. No judgment was produced for this ticket. It counts as an error in the run total.

Solve

—

Wall-clock time in the container

Full test suite

Not run

The harness did not reach the test stage

Contamination audit

Unknown

See the audit notes for this run

Patch size

0vs 0 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+0 −0
No patch was produced.
Merged fix · PR #+0 −0
No patch was produced.

What the agent reported

Failed to authenticate: OAuth session expired and could not be refreshed