Backtestspydanticrun-20260929-204949#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 apply the identical root-cause fix: on Python <= 3.13, read a type's own annotations via `obj.__dict__.get('__annotations__', {})` instead of `getattr`, which is exactly the fix proposed in the issue. Correctness is a wash. The difference is in tests and craft. Patch B's test reproduces the actual reported symptom end-to-end: it parametrizes a generic model under a poisoned metaclass and asserts both that the typevar is substituted (`model_fields['x'].annotation is str`) and that the default is preserved (`m.a == 5`). This directly exercises the two failure modes the issue describes and would fail before the fix. Patch A's test only checks the internal helper (`safe_get_annotations(Sub) == {}`); it is a valid regression test (it would return the parent's `{'f': int}` before the fix), but it verifies the mechanism rather than the user-facing behavior, so it is weaker evidence that the actual bug is resolved. Patch B also adds an explanatory comment inline at the fix site describing the CPython <= 3.13 annotation-auto-creation quirk and the langgraph/ABCMeta trigger, aiding future maintainers; Patch A's fix has no comment. Both tests isolate the poisoning to a local metaclass, avoiding global state pollution. Patch B is the better fix on tests and craft.
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
5m 57s22 turns
Wall-clock time in the container
Full test suite
No new failures
12,274 passed · 501 skipped · 12,775 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
35vs 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..5b8cfd5f 100644@@ -293,6 +293,13 @@ def safe_get_annotations(obj: Any) -> dict[str, Any]: """ 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 that has none auto-creates a+ # plain `__annotations__ = {}` entry in the class's `__dict__`. If such an entry is planted+ # on a metaclass (e.g. via a bare `getattr(meta, '__annotations__', None)`), a plain+ # `getattr` on a model class can then leak a parent class's annotations. Read the class's+ # own annotations directly, as `inspect.get_annotations()` does, to avoid this:+ return obj.__dict__.get('__annotations__', {}) else: return getattr(obj, '__annotations__', {}) tests/test_generics.pyindex a0c46632..10dbcafc 100644@@ -3154,6 +3154,34 @@ def test_generic_with_allow_extra(): data: T +def test_generic_with_poisoned_metaclass_annotations():+ # Regression test: on CPython <= 3.13, reading `__annotations__` on a metaclass in the+ # model's metatype MRO plants a `{}` entry in its `__dict__`, which used to leak the parent+ # model's annotations to freshly-parametrized generic submodels, dropping typevar+ # substitution and field defaults (e.g. `import langgraph.prebuilt` plants such an entry+ # on `abc.ABCMeta` at import time).+ from pydantic._internal._model_construction import ModelMetaclass++ class PoisonedMeta(ModelMetaclass):+ pass++ # Trigger the poisoning read on our own metaclass only.+ PoisonedMeta.__annotations__++ T = TypeVar('T')++ class G(BaseModel, Generic[T], metaclass=PoisonedMeta):+ x: T+ a: int = 5++ parametrized = G[str]+ # Typevar must be substituted:+ assert parametrized.model_fields['x'].annotation is str+ # Field default must be preserved (field stays optional):+ m = parametrized.model_validate({'x': 'hi'})+ assert m.a == 5++ def test_generic_field(): """Test for https://github.com/pydantic/pydantic/issues/10039.
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
All done. Here is the summary.
What was wrong
On CPython ≤ 3.13, reading __annotations__ on a metaclass in a model's metatype MRO (e.g. via getattr(meta, '__annotations__', None), as langgraph does at import time on abc.ABCMeta) auto-creates a plain __annotations__ = {} entry in that metaclass's __dict__. That plain dict shadows the type.__annotations__ data descriptor, so getattr(ModelClass, '__annotations__') on a freshly-parametrized generic submodel — which has no own __annotations__ — falls back through the model's own MRO and returns the parent class's annotations instead of {}.
safe_get_annotations() in pydantic/_internal/_typing_extra.py used getattr(obj, '__annotations__', {}), so this leak made collect_model_fields() treat every inherited field as locally-annotated, skipping typevar substitution (x: T → Any) and dropping defaults (defaulted fields became required).
What I changed
pydantic/_internal/_typing_extra.py— Insafe_get_annotations, on the ≤ 3.13 path, added anelif isinstance(obj, type):branch that reads a class's own annotations directly viaobj.__dict__.get('__annotations__', {})(whatinspect.get_annotations()does), bypassing the poisoned metaclass descriptor. Thegetattrfallback is retained for non-class objects — critically, functions store__annotations__outside__dict__, so they must (and do) keep usinggetattr.tests/test_generics.py— Addedtest_generic_with_poisoned_metaclass_annotations, which triggers the poisoning on a localModelMetaclasssubclass (avoiding global test-isolation pollution), then asserts typevar substitution (model_fields['x'].annotation is str) and default preservation (m.a == 5).
Test evidence
- Regression test fails before the fix (
assert ~T is str→AssertionError) and passes after. - Gate:
./bt-test tests/test_generics.py tests/test_dataclasses.py tests/test_deferred_annotations.py tests/test_edge_cases.py→ 583 passed, 14 skipped, 5 xfailed — no previously passing test broke. - Reviewer confirmed the fix matches the report, preserves function-annotation behavior, and the test is valid and isolated.
Requirements disposition
- Fix the dropped typevar substitution and field defaults after a metaclass
__annotations__read (≤ 3.13) — ✅ implemented; regression test asserts both. - Suggested approach: read own annotations via
obj.__dict__.get('__annotations__', {})rather thangetattrfor classes on ≤ 3.13 — ✅ implemented exactly, scoped toisinstance(obj, type)so both class call sites (get_model_type_hints,collect_model_fields/cls_annotations) get own-class annotations while function call sites are untouched. - Python 3.14 unaffected — ✅ the
>= 3.14branch is unchanged; the test also passes there since poisoning doesn't occur.
Changes are left uncommitted in the working tree.