Backtestspydanticrun-20261027-165153#13919
TypedDict closed/extra_items (PEP 728) not inherited by subclasses; extra_items=Never and ReadOnly[...] fail
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the three reported cases for plain inheritance, Never, and ReadOnly, and both pass 12 of 13 independent tests. The difference is generic parents: Patch B fails `test_extra_items_inherited_from_parametrized_generic_parent` (Child(Parent[int]) with extra_items=T), because B looks up `__extra_items__` through the virtual MRO of bare classes and discards the type arguments supplied in `__orig_bases__`, leaving an unsubstituted TypeVar. Patch A walks `__orig_bases__` directly, builds a per-base typevars map from the parametrization, and substitutes correctly; it also handles NoReturn as an alias of Never, protects against cycles, and respects an explicit `closed=False` on a subclass. Patch A's tests are substantially broader (grandchild, multiple bases, generic/parametrized parents, ReadOnly[Never], generic Never, inherited config warning). Patch B adds an unrequested UserWarning for ReadOnly extra items, which the PEP explicitly allows and the issue did not ask for, adding noise rather than value. A is the more correct and better-tested fix.
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 38s
Wall-clock time in the container
Full test suite
No new failures
613 passed · 32 skipped · 645 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
216vs 122 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
PEP 728 support was added in #12179, but a few cases from the spec don't work.
extra_itemsisn't inherited. A subclass of a TypedDict withextra_items=int
should also accept extra int items. Pydantic treats the subclass as a plain TypedDict
and silently drops the extra keys.
closed=Trueisn't inherited. A subclass of a closed TypedDict should also reject
extra keys. Pydantic accepts them and drops them instead of raising extra_forbidden.
This also affects JSON schema: the parent gets "additionalProperties": false,
the subclass doesn't.
extra_items=Neverandextra_items=ReadOnly[int]raise
PydanticSchemaGenerationError. The PEP says extra_items=Never is the same asclosed=True, and allows extra_items to be wrapped in ReadOnly[].
For 1 and 2, I think the cause is that _generate_schema.py reads __closed__ and__extra_items__ directly from the class. Per the PEP, those attributes only reflect
what was passed to that exact class, not its parents. TypedDict parents also aren't
in __mro__, only in __orig_bases__, so finding the inherited value means walking
those.
For 3, extra_items is passed straight to generate_schema without handlingNever or unwrapping ReadOnly.
I'd like to work on a fix for this if that's okay.
Example Code
import json
from typing import Never
from typing_extensions import ReadOnly, TypedDict
from pydantic import TypeAdapter
class Parent(TypedDict, extra_items=int):
a: str
class Child(Parent):
pass
print(TypeAdapter(Parent).validate_python({'a': 'x', 'b': '1'}))
# {'a': 'x', 'b': 1}
print(TypeAdapter(Child).validate_python({'a': 'x', 'b': '1'}))
# {'a': 'x'} expected {'a': 'x', 'b': 1}
class ClosedParent(TypedDict, closed=True):
a: str
class ClosedChild(ClosedParent):
pass
print(TypeAdapter(ClosedChild).validate_python({'a': 'x', 'b': 1}))
# {'a': 'x'} expected ValidationError
print(json.dumps(TypeAdapter(ClosedParent).json_schema()))
# includes "additionalProperties": false
print(json.dumps(TypeAdapter(ClosedChild).json_schema()))
# missing "additionalProperties": false
TypeAdapter(TypedDict('TD', {'a': str}, extra_items=Never))
# PydanticSchemaGenerationError
TypeAdapter(TypedDict('TD', {'a': str}, extra_items=ReadOnly[int]))
# PydanticSchemaGenerationError
Python, Pydantic & OS Version
pydantic version: 2.13.5
pydantic-core version: 2.46.5
pydantic-core build: profile=release pgo=false
python version: 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)]
platform: Windows-11-10.0.26100-SP0
related packages: typing_extensions-4.16.0
commit: unknown
Also reproduced on Linux with Python 3.15.0b4 and pydantic main (2.14.0b2).The two patches
pydantic/_internal/_generate_schema.pyindex a446559b..fde382b3 100644@@ -1482,15 +1482,13 @@ class GenerateSchema: extra_behavior: core_schema.ExtraBehavior = 'ignore' extras_schema: CoreSchema | None = None # For 'allow', equivalent to `Any` - no validation performed. - # `__closed__` is `None` when not specified (equivalent to `False`):- is_closed = bool(getattr(typed_dict_cls, '__closed__', False))- extra_items = getattr(typed_dict_cls, '__extra_items__', typing_extensions.NoExtraItems)+ is_closed, extra_items = _get_typed_dict_extra_items(typed_dict_cls, typevars_map) if is_closed: extra_behavior = 'forbid' extras_schema = None elif not typing_objects.is_noextraitems(extra_items): extra_behavior = 'allow'- extras_schema = self.generate_schema(replace_types(extra_items, typevars_map))+ extras_schema = self.generate_schema(extra_items) if (config_extra := self._config_wrapper.extra) in ('allow', 'forbid'): if is_closed and config_extra == 'allow':@@ -2556,6 +2554,60 @@ _VALIDATOR_F_MATCH: Mapping[ } +def _get_typed_dict_extra_items(typed_dict_cls: Any, typevars_map: Mapping[Any, Any] | None) -> tuple[bool, Any]:+ """Get the (PEP 728) closed state and extra items type of a `TypedDict` class, taking inheritance into account.++ The `__closed__` and `__extra_items__` attributes only reflect the arguments passed to the class itself,+ so the value has to be looked up in the (original) bases if not specified.++ Returns:+ A two-tuple, containing whether the `TypedDict` is closed and the extra items type (`NoExtraItems` if+ unspecified or if the `TypedDict` is closed). Type variables are replaced using `typevars_map`+ (for the class itself) and by the arguments used to parametrize generic bases.+ """+ is_closed, extra_items = _find_typed_dict_extra_items(typed_dict_cls, typevars_map, set())+ if not is_closed and not typing_objects.is_noextraitems(extra_items):+ # Extra items can be marked as read-only, which has no impact on validation:+ if typing_objects.is_readonly(get_origin(extra_items)):+ extra_items = get_args(extra_items)[0]+ # `extra_items=Never` is equivalent to `closed=True`:+ if typing_objects.is_never(extra_items) or typing_objects.is_noreturn(extra_items):+ is_closed, extra_items = True, typing_extensions.NoExtraItems+ return is_closed, extra_items+++def _find_typed_dict_extra_items(+ typed_dict_cls: Any, typevars_map: Mapping[Any, Any] | None, seen: set[Any]+) -> tuple[bool, Any]:+ if typed_dict_cls in seen:+ return False, typing_extensions.NoExtraItems+ seen.add(typed_dict_cls)++ # `__closed__` is `None` when not specified:+ closed = getattr(typed_dict_cls, '__closed__', None)+ if closed:+ return True, typing_extensions.NoExtraItems+ extra_items = getattr(typed_dict_cls, '__extra_items__', typing_extensions.NoExtraItems)+ if not typing_objects.is_noextraitems(extra_items):+ return False, replace_types(extra_items, typevars_map)+ if closed is not None:+ # Explicitly set to `closed=False`.+ return False, typing_extensions.NoExtraItems++ for base in getattr(typed_dict_cls, '__orig_bases__', ()):+ base_origin = get_origin(base) or base+ if not is_typeddict(base_origin):+ continue+ base_typevars_map = {+ param: replace_types(arg, typevars_map)+ for param, arg in zip(getattr(base_origin, '__parameters__', ()), get_args(base), strict=False)+ }+ is_closed, base_extra_items = _find_typed_dict_extra_items(base_origin, base_typevars_map, seen)+ if is_closed or not typing_objects.is_noextraitems(base_extra_items):+ return is_closed, base_extra_items+ return False, typing_extensions.NoExtraItems++ # TODO V3: this function is only used for deprecated decorators. It should # be removed once we drop support for those. def apply_validators(tests/test_types_typeddict.pyindex 697913f2..aa1fa776 100644@@ -1053,6 +1053,162 @@ def test_typeddict_extraitems_generic() -> None: assert exc.value.errors()[0]['loc'] == ('extra',) +def test_typeddict_extraitems_inherited() -> None:+ class Parent(TypedDict, extra_items=int):+ a: str++ class Child(Parent):+ pass++ class GrandChild(Child):+ b: str++ for td in (Parent, Child):+ ta = TypeAdapter(td)+ assert ta.validate_python({'a': 'x', 'b': '1'}) == {'a': 'x', 'b': 1}+ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'a': 'x', 'b': 'not_an_int'})+ assert exc.value.errors()[0]['loc'] == ('b',)+ assert ta.json_schema()['additionalProperties'] == {'type': 'integer'}++ ta = TypeAdapter(GrandChild)+ assert ta.validate_python({'a': 'x', 'b': 'y', 'c': '1'}) == {'a': 'x', 'b': 'y', 'c': 1}+ assert ta.json_schema()['additionalProperties'] == {'type': 'integer'}+++def test_typeddict_closed_inherited() -> None:+ class ClosedParent(TypedDict, closed=True):+ a: str++ class ClosedChild(ClosedParent):+ pass++ class ClosedGrandChild(ClosedChild):+ b: str++ for td, data in ((ClosedChild, {'a': 'x'}), (ClosedGrandChild, {'a': 'x', 'b': 'y'})):+ ta = TypeAdapter(td)+ assert ta.validate_python(data) == data+ with pytest.raises(ValidationError) as exc:+ ta.validate_python({**data, 'extra': 1})+ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+ assert ta.json_schema()['additionalProperties'] is False+++def test_typeddict_extraitems_inherited_multiple_bases() -> None:+ class Open(TypedDict):+ a: str++ class Extra(TypedDict, extra_items=int):+ b: str++ class Child(Open, Extra):+ pass++ assert TypeAdapter(Child).validate_python({'a': 'x', 'b': 'y', 'c': '1'}) == {'a': 'x', 'b': 'y', 'c': 1}+++def test_typeddict_explicitly_open_not_inherited() -> None:+ class Parent(TypedDict):+ a: str++ class Child(Parent, closed=False):+ pass++ assert TypeAdapter(Child).validate_python({'a': 'x', 'b': 1}) == {'a': 'x'}+++def test_typeddict_extraitems_inherited_generic() -> None:+ T = TypeVar('T')++ class Parent(TypedDict, Generic[T], extra_items=T):+ a: str++ class Child(Parent[int]):+ pass++ class GenericChild(Parent[list[T]], Generic[T]):+ pass++ assert TypeAdapter(Child).validate_python({'a': 'x', 'b': '1'}) == {'a': 'x', 'b': 1}+ with pytest.raises(ValidationError):+ TypeAdapter(Child).validate_python({'a': 'x', 'b': 'not_an_int'})++ ta = TypeAdapter(GenericChild[int])+ assert ta.validate_python({'a': 'x', 'b': ['1']}) == {'a': 'x', 'b': [1]}+ with pytest.raises(ValidationError):+ ta.validate_python({'a': 'x', 'b': ['not_an_int']})+++@pytest.mark.parametrize('never', [typing_extensions.Never, typing.NoReturn])+def test_typeddict_extraitems_never(never: Any) -> None:+ class TD(TypedDict, extra_items=never):+ a: str++ class Child(TD):+ pass++ for td in (TD, TypedDict('FunctionalTD', {'a': str}, extra_items=never), Child):+ ta = TypeAdapter(td)+ assert ta.validate_python({'a': 'x'}) == {'a': 'x'}+ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'a': 'x', 'b': 1})+ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+ assert ta.json_schema()['additionalProperties'] is False+++def test_typeddict_extraitems_readonly() -> None:+ class TD(TypedDict, extra_items=ReadOnly[int]):+ a: str++ class Child(TD):+ pass++ for td in (TD, Child):+ ta = TypeAdapter(td)+ assert ta.validate_python({'a': 'x', 'b': '1'}) == {'a': 'x', 'b': 1}+ with pytest.raises(ValidationError):+ ta.validate_python({'a': 'x', 'b': 'not_an_int'})+ assert ta.json_schema()['additionalProperties'] == {'type': 'integer'}++ ta = TypeAdapter(TypedDict('TD', {'a': str}, extra_items=ReadOnly[typing_extensions.Never]))+ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'a': 'x', 'b': 1})+ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+++def test_typeddict_extraitems_generic_never() -> None:+ T = TypeVar('T')++ class TD(TypedDict, Generic[T], extra_items=T):+ a: str++ class ReadOnlyTD(TypedDict, Generic[T], extra_items=ReadOnly[T]):+ a: str++ for td in (TD[typing_extensions.Never], ReadOnlyTD[typing_extensions.Never]):+ ta = TypeAdapter(td)+ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'a': 'x', 'b': 1})+ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+ assert ta.json_schema()['additionalProperties'] is False++ ta = TypeAdapter(ReadOnlyTD[int])+ assert ta.validate_python({'a': 'x', 'b': '1'}) == {'a': 'x', 'b': 1}+++def test_typeddict_inherited_extra_config_warning() -> None:+ class Closed(TypedDict, closed=True):+ f: int++ @with_config(extra='allow')+ class ClosedChild(Closed):+ pass++ with pytest.warns(TypedDictExtraConfigWarning):+ TypeAdapter(ClosedChild)++ def test_typeddict_incompatible_extra_config_warning() -> None: @with_config(extra='allow') class TD1(TypedDict, closed=True):
pydantic/_internal/_generate_schema.pyindex a446559b8..0d03cd299 100644@@ -98,6 +98,7 @@ from ._decorators import ( inspect_field_serializer, inspect_model_serializer, inspect_validator,+ mro, ) from ._docs_extraction import extract_docstrings_from_cls from ._fields import (@@ -317,6 +318,37 @@ GENERATE_SCHEMA_ERRORS = ( """Errors raised during core schema generation. This does *not* include `InvalidSchemaError`, which is raised during schema cleaning.""" +def _get_typed_dict_extra_items(typed_dict_cls: type[Any]) -> tuple[bool, Any, bool]:+ """Return whether the TypedDict class is closed, the type of its extra items and whether+ they are read-only (as per PEP 728).++ The `__closed__` and `__extra_items__` attributes only reflect the arguments passed to the class itself,+ so the first class in the (virtual) MRO explicitly specifying one of them is used.+ `extra_items=Never` is normalized to `closed=True`, and the `ReadOnly` qualifier is stripped.+ """+ for base in mro(typed_dict_cls):+ # `__closed__` is `None` when not specified:+ closed = base.__dict__.get('__closed__')+ extra_items = base.__dict__.get('__extra_items__', typing_extensions.NoExtraItems)+ if closed is not None or not typing_objects.is_noextraitems(extra_items):+ break+ else:+ return False, typing_extensions.NoExtraItems, False++ if closed is not None:+ # `closed` and `extra_items` can't be used together:+ return closed, typing_extensions.NoExtraItems, False++ readonly = typing_objects.is_readonly(get_origin(extra_items))+ if readonly:+ extra_items = get_args(extra_items)[0]+ # TODO: `Never` wrapped in a type alias (e.g. `type NoExtras = Never`) or in `Annotated` isn't detected here,+ # and fails during schema generation. Unwrapping should only apply to this check (to preserve alias handling).+ if typing_objects.is_never(extra_items):+ return True, typing_extensions.NoExtraItems, False+ return False, extra_items, readonly++ class InvalidSchemaError(Exception): """The core schema is invalid.""" @@ -1482,9 +1514,7 @@ class GenerateSchema: extra_behavior: core_schema.ExtraBehavior = 'ignore' extras_schema: CoreSchema | None = None # For 'allow', equivalent to `Any` - no validation performed. - # `__closed__` is `None` when not specified (equivalent to `False`):- is_closed = bool(getattr(typed_dict_cls, '__closed__', False))- extra_items = getattr(typed_dict_cls, '__extra_items__', typing_extensions.NoExtraItems)+ is_closed, extra_items, extra_items_readonly = _get_typed_dict_extra_items(typed_dict_cls) if is_closed: extra_behavior = 'forbid' extras_schema = None@@ -1492,6 +1522,13 @@ class GenerateSchema: extra_behavior = 'allow' extras_schema = self.generate_schema(replace_types(extra_items, typevars_map)) + if extra_items_readonly:+ warnings.warn(+ f'Extra items on TypedDict class {typed_dict_cls.__name__!r} are using the `ReadOnly` qualifier. '+ 'Pydantic will not protect items from any mutation on dictionary instances.',+ UserWarning,+ )+ if (config_extra := self._config_wrapper.extra) in ('allow', 'forbid'): if is_closed and config_extra == 'allow': warnings.warn(tests/test_types_typeddict.pyindex 697913f2b..700ff7257 100644@@ -10,7 +10,7 @@ import pytest import typing_extensions from annotated_types import Lt from pydantic_core import core_schema-from typing_extensions import NotRequired, ReadOnly, Required, TypedDict+from typing_extensions import Never, NotRequired, ReadOnly, Required, TypedDict from pydantic import ( BaseModel,@@ -1053,6 +1053,83 @@ def test_typeddict_extraitems_generic() -> None: assert exc.value.errors()[0]['loc'] == ('extra',) +def test_typeddict_closed_inherited() -> None:+ """https://github.com/pydantic/pydantic/issues/13919"""++ class Parent(TypedDict, closed=True):+ f: int++ class Child(Parent):+ pass++ ta = TypeAdapter(Child)++ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'f': 1, 'extra': 1})++ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+ assert ta.json_schema()['additionalProperties'] is False+++def test_typeddict_extraitems_inherited() -> None:+ """https://github.com/pydantic/pydantic/issues/13919"""++ class Parent(TypedDict, extra_items=int):+ f: int++ class Child(Parent):+ g: NotRequired[int]++ class GrandChild(Child):+ pass++ ta = TypeAdapter(GrandChild)++ assert ta.validate_python({'f': 1, 'g': 1, 'extra': '1'}) == {'f': 1, 'g': 1, 'extra': 1}+ assert ta.json_schema()['additionalProperties'] == {'type': 'integer'}+++def test_typeddict_extraitems_overridden() -> None:+ class Parent(TypedDict, extra_items=ReadOnly[float]):+ f: int++ class Child(Parent, extra_items=int):+ pass++ assert TypeAdapter(Child).json_schema()['additionalProperties'] == {'type': 'integer'}+++def test_typeddict_extraitems_never() -> None:+ class TD(TypedDict, extra_items=Never):+ f: int++ ta = TypeAdapter(TD)++ with pytest.raises(ValidationError) as exc:+ ta.validate_python({'f': 1, 'extra': 1})++ assert exc.value.errors()[0]['type'] == 'extra_forbidden'+ assert ta.json_schema()['additionalProperties'] is False+++def test_typeddict_extraitems_readonly() -> None:+ class TD(TypedDict, extra_items=ReadOnly[int]):+ f: int++ with pytest.warns(UserWarning, match="Extra items on TypedDict class 'TD' are using the `ReadOnly` qualifier"):+ ta = TypeAdapter(TD)++ assert ta.validate_python({'f': 1, 'extra': '1'}) == {'f': 1, 'extra': 1}+++def test_typeddict_extraitems_readonly_never() -> None:+ class TD(TypedDict, extra_items=ReadOnly[Never]):+ f: int++ # No `ReadOnly` warning, as no extra items are allowed:+ assert TypeAdapter(TD).json_schema()['additionalProperties'] is False++ def test_typeddict_incompatible_extra_config_warning() -> None: @with_config(extra='allow') class TD1(TypedDict, closed=True):