Backtestspydanticrun-20260923-170312#13520
Generic model parametrization drops field defaults and typevar substitution after __annotations__ is read on a metaclass (Python <= 3.13)
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches make the identical root-cause fix: on Python <= 3.13, read a class's own annotations via `obj.__dict__.get('__annotations__', {})` instead of `getattr`, so the metaclass-planted `{}` that shadows the `type.__annotations__` descriptor can no longer leak the parent's annotations. Correctness is equivalent between them. The differences are in craft and tests. On craft, B is cleaner: it extends the existing `if/elif/else` chain with a flat `elif isinstance(obj, type)` branch and updates the function docstring plus adds an inline comment explaining exactly why the class case is special, whereas A nests a fresh `if/else` inside the pre-3.14 `else` block with no explanation. On tests, both write a test that fails before and passes after. A's test is a narrow unit assertion (`safe_get_annotations(Sub) == {}`) using a bare `Meta(type)`; it correctly exercises the fixed function but is one abstraction level removed from the reported symptom. B's test reproduces the actual reported regression end-to-end through `ModelMetaclass`, reads `Meta.__annotations__`, then parametrizes `G[str]` for the first time afterward and asserts both that the typevar is substituted (`m.x == 'hi'`) and that the defaulted field survives (`m.a == 5`) — precisely the two failure modes the issue calls out. That makes B's test a stronger guard against the real bug. Scope is minimal in both. B edges A on craft and test fidelity with equal correctness.
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 6s12 turns
Wall-clock time in the container
Full test suite
No new failures
6,428 passed · 368 skipped · 6,796 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
33vs 26 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
This is a regression on main (2.14.0a1); I could not reproduce it on any released version (checked 2.13.4 and 2.10.6 under the same conditions). It affects Python 3.10–3.13 (verified on 3.10, 3.12 and 3.13); Python 3.14 is unaffected.
If __annotations__ has ever been read on a metaclass appearing in a model's metatype MRO (e.g. ModelMetaclass itself, or abc.ABCMeta), then any generic model parametrized for the first time afterwards gets a broken schema:
- type variables are not substituted (
x: Tends up with ananyschema), and - field defaults are dropped (fields with defaults become required).
Parametrizations built before the read keep working, since their schemas are already cached — which makes this very order-dependent and confusing to track down in a real test suite.
Reading an attribute sounds like an odd trigger, but it happens in the wild: on CPython ≤ 3.13, reading __annotations__ on a (heap) class that has none auto-creates a plain __annotations__ = {} entry in that class's __dict__ (see the annotations HOWTO). langgraph does such a read at import time (get_cached_annotated_keys in langgraph/_internal/_fields.py has a getattr(base, "__annotations__", None) fallback — added for Python 3.14 compat, ironically — which ends up planting {} on abc.ABCMeta). So a plain import langgraph.prebuilt breaks all subsequently-parametrized generic models. We found this via the pydantic/logfire weekly CI job testing against pydantic main, where pydantic_evals.Dataset.from_dict() fails with 3 validation errors for _DatasetModel[...] — Field required for exactly its three defaulted fields — but only in test runs where a langchain test ran first (failing run).
Mechanism:
- The planted
{}lives in the metaclass's__dict__. For a model classM, attribute lookup ofM.__annotations__searchestype(M).__mro__(e.g.(ModelMetaclass, ABCMeta, type, object)) and now finds the plain dict before reaching thetype.__annotations__getset descriptor. A plain dict is not a data descriptor, so lookup falls back toM's own MRO — and for a class with no own__annotations__(like a freshly created generic submodel), this returns the parent's annotations. (Normally thetype.__annotations__data descriptor intercepts the lookup and auto-creates/returns the class's own empty dict, which is what makesgetattrlook safe.) safe_get_annotations()inpydantic/_internal/_typing_extra.pyusesgetattr(obj, '__annotations__', {})on < 3.14, so for the new submodelG[str]it returns the origin's annotations instead of{}:clean: safe_get_annotations(G[str]) == {} poisoned: safe_get_annotations(G[str]) == {'x': ~T, 'a': <class 'int'>}- In
collect_model_fields(),cls_annotationsis "only used to determine if an annotation comes from a parent class". The leak makesann_name in cls_annotationstrue for every inherited field, so instead of copying the parentFieldInfoand applyingtypevars_map(via_recreate_field_info), it takes the plainFieldInfo.from_annotation(ann_type)branch: no typevar substitution, and no default — the origin's class-attribute default is long gone because it wasdelattr'd during the origin's own field collection.
Possible fix: for classes on ≤ 3.13, read own annotations via obj.__dict__.get('__annotations__', {}) (what inspect.get_annotations() does) rather than getattr. Both call sites of safe_get_annotations() seem to want own-class annotations anyway: get_model_type_hints() iterates the MRO itself and calls it per base, and cls_annotations in collect_model_fields() is explicitly meant to be the current class's own annotations.
Example Code
from typing import Generic, TypeVar
from pydantic import BaseModel
# The trigger: any read of `__annotations__` on a metaclass. On CPython <= 3.13 this
# auto-creates a plain `__annotations__ = {}` entry in the metaclass's `__dict__`,
# which shadows the `type.__annotations__` data descriptor for every model class.
# `abc.ABCMeta.__annotations__` has the same effect, and `import langgraph.prebuilt`
# performs an equivalent read at import time.
type(BaseModel).__annotations__
T = TypeVar('T')
class G(BaseModel, Generic[T]):
x: T
a: int = 5
G[str].model_validate({'x': 'hi'})
pydantic_core._pydantic_core.ValidationError: 1 validation error for G[str]
a
Field required [type=missing, input_value={'x': 'hi'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.14/v/missing
Removing the type(BaseModel).__annotations__ line — or moving the G[str] parametrization above it — makes it pass. To run against main:
uv run --isolated --no-project --python 3.13 --with "pydantic @ git+https://github.com/pydantic/pydantic.git" repro.py
Python, Pydantic & OS Version
pydantic version: 2.14.0a1
pydantic-core version: 2.47.0
pydantic-core build: profile=release pgo=false
python version: 3.13.0 (main, Oct 12 2024, 20:24:27) [Clang 15.0.0 (clang-1500.3.9.4)]
platform: macOS-26.5.1-arm64-arm-64bit-Mach-O
related packages: mypy-2.3.0 pyright-1.1.411 email-validator-2.3.0 fastapi-0.139.2 pydantic-settings-2.14.2 typing_extensions-4.16.0
commit: a187a65467a588ccc00761d80f325b45381ca819
Selected Assignee: @Viicos
The two patches
pydantic/_internal/_typing_extra.pyindex ab6a1397..4f6a96ad 100644@@ -290,9 +290,18 @@ def safe_get_annotations(obj: Any) -> dict[str, Any]: Starting with Python 3.14, accessing the `__annotations__` attribute might raise a `NameError` if a referenced symbol isn't defined yet. In this case, we return the annotation in the *forward ref* format.++ On Python <= 3.13, for classes we read the class's own annotations directly (matching what+ `inspect.get_annotations()` does), to avoid accidentally returning an inherited parent's annotations. """ if sys.version_info >= (3, 14): return annotationlib.get_annotations(obj, format=annotationlib.Format.FORWARDREF)+ elif isinstance(obj, type):+ # On Python <= 3.13, reading `__annotations__` on a class via `getattr` can return an+ # inherited parent's annotations if a metaclass in the MRO has a planted `__annotations__`+ # entry shadowing the `type.__annotations__` descriptor. Read the class's own annotations+ # directly (matching what `inspect.get_annotations()` does):+ return obj.__dict__.get('__annotations__', {}) else: return getattr(obj, '__annotations__', {}) tests/test_generics.pyindex a0c46632..0fac7ba7 100644@@ -3271,3 +3271,27 @@ def test_slots_forwarded_from_generic_class() -> None: with pytest.raises(TypeError): # As per https://docs.python.org/3/reference/datamodel.html#slots: weakref.ref(BaseInt())+++def test_generic_parametrization_after_metaclass_annotations_read():+ from pydantic._internal._model_construction import ModelMetaclass++ class Meta(ModelMetaclass):+ pass++ T = TypeVar('T')++ class G(BaseModel, Generic[T], metaclass=Meta):+ x: T+ a: int = 5++ # Reading `__annotations__` on the metaclass plants a plain `{}` in+ # `Meta.__dict__` on CPython <= 3.13, shadowing the `type.__annotations__`+ # data descriptor.+ Meta.__annotations__++ # First parametrization AFTER the read must still substitute the typevar and+ # keep field defaults.+ m = G[str].model_validate({'x': 'hi'})+ assert m.x == 'hi'+ assert m.a == 5
pydantic/_internal/_typing_extra.pyindex ab6a1397c..d68c792af 100644@@ -294,7 +294,10 @@ def safe_get_annotations(obj: Any) -> dict[str, Any]: if sys.version_info >= (3, 14): return annotationlib.get_annotations(obj, format=annotationlib.Format.FORWARDREF) else:- return getattr(obj, '__annotations__', {})+ if isinstance(obj, type):+ return obj.__dict__.get('__annotations__', {})+ else:+ return getattr(obj, '__annotations__', {}) def get_model_type_hints(tests/test_edge_cases.pyindex 3e8e38e7d..df52288cc 100644@@ -51,6 +51,7 @@ from pydantic import ( validator, ) from pydantic._internal._model_construction import ModelMetaclass+from pydantic._internal._typing_extra import safe_get_annotations from pydantic.fields import Field, computed_field from pydantic.functional_serializers import ( field_serializer,@@ -3161,3 +3162,23 @@ def test_model_fields_set_includes_extra_after_assignment(): m.extra_after_init = 3 assert m.model_fields_set == {'field', 'extra_at_init', 'extra_after_init'} assert m.model_extra == {'extra_at_init': 2, 'extra_after_init': 3}+++def test_safe_get_annotations_from_dict() -> None:+ """https://github.com/pydantic/pydantic/issues/13520"""++ class Meta(type):+ pass++ Meta.__annotations__++ class Base(metaclass=Meta):+ pass++ class Main(Base):+ f: int++ class Sub(Main):+ pass++ assert safe_get_annotations(Sub) == {}
What the agent reported
The fix is complete and verified.
Summary
What was wrong: On CPython ≤ 3.13, reading __annotations__ on a metaclass in a model's metatype MRO auto-plants a plain __annotations__ = {} in that metaclass's __dict__, which shadows the type.__annotations__ data descriptor. After that, safe_get_annotations() in pydantic/_internal/_typing_extra.py — which used getattr(obj, '__annotations__', {}) — returned an inherited parent class's annotations instead of the object's own. In collect_model_fields(), this made every inherited field look like it was declared on the current class, so first-time generic parametrization skipped typevar substitution (x: T → any) and dropped field defaults (defaulted fields became required).
What I changed: In pydantic/_internal/_typing_extra.py, safe_get_annotations now, on the < 3.14 path, reads a class's own annotations directly via obj.__dict__.get('__annotations__', {}) when isinstance(obj, type) — mirroring inspect.get_annotations(). The getattr path is preserved for non-classes (function objects, whose annotations live outside __dict__ and which get_function_type_hints passes in). The 3.14 branch is untouched. One-file production change plus a docstring note.
Test evidence:
- Added
test_generic_parametrization_after_metaclass_annotations_readintests/test_generics.py, which reads__annotations__on a localModelMetaclasssubclass, then parametrizes a generic model and asserts both typevar substitution (x == 'hi') and default preservation (a == 5). The local metaclass isolates the poisoning so it can't leak to other tests. - Before the fix: the test failed with the exact reported error (
a — Field required). - After the fix: gate run on
tests/test_generics.py tests/test_deferred_annotations.py tests/test_edge_cases.py→ 319 passed, 11 skipped, 4 xfailed on Python 3.13.15, including the new test. - Reviewer confirmed the diff is minimal, correct, and complete with no unrelated changes.
Requirements disposition:
- Fix the regression (typevar substitution + defaults dropped after metaclass
__annotations__read) — Implemented, with a failing-then-passing regression test. - Suggested fix: read own annotations via
obj.__dict__.get('__annotations__', {})on ≤ 3.13 for classes — Implemented as suggested, gated onisinstance(obj, type)to protect the function-object caller. - Initial check "using Pydantic V2" — informational only; no action needed.
All changes are left uncommitted in the working tree.