Skip to content

Commit e2456d3

Browse files
committed
Support closed and extra_items via inheritance in TypedDict
Backport of: #13967
1 parent 317655d commit e2456d3

2 files changed

Lines changed: 118 additions & 4 deletions

File tree

‎pydantic/_internal/_generate_schema.py‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
inspect_field_serializer,
9999
inspect_model_serializer,
100100
inspect_validator,
101+
mro,
101102
)
102103
from ._docs_extraction import extract_docstrings_from_cls
103104
from ._fields import (
@@ -317,6 +318,37 @@ def _add_custom_serialization_from_json_encoders(
317318
"""Errors raised during core schema generation. This does *not* include `InvalidSchemaError`, which is raised during schema cleaning."""
318319

319320

321+
def _get_typed_dict_extra_items(typed_dict_cls: type[Any]) -> tuple[bool, Any, bool]:
322+
"""Return whether the TypedDict class is closed, the type of its extra items and whether
323+
they are read-only (as per PEP 728).
324+
325+
The `__closed__` and `__extra_items__` attributes only reflect the arguments passed to the class itself,
326+
so the first class in the (virtual) MRO explicitly specifying one of them is used.
327+
`extra_items=Never` is normalized to `closed=True`, and the `ReadOnly` qualifier is stripped.
328+
"""
329+
for base in mro(typed_dict_cls):
330+
# `__closed__` is `None` when not specified:
331+
closed = base.__dict__.get('__closed__')
332+
extra_items = base.__dict__.get('__extra_items__', typing_extensions.NoExtraItems)
333+
if closed is not None or not typing_objects.is_noextraitems(extra_items):
334+
break
335+
else:
336+
return False, typing_extensions.NoExtraItems, False
337+
338+
if closed is not None:
339+
# `closed` and `extra_items` can't be used together:
340+
return closed, typing_extensions.NoExtraItems, False
341+
342+
readonly = typing_objects.is_readonly(get_origin(extra_items))
343+
if readonly:
344+
extra_items = get_args(extra_items)[0]
345+
# TODO: `Never` wrapped in a type alias (e.g. `type NoExtras = Never`) or in `Annotated` isn't detected here,
346+
# and fails during schema generation. Unwrapping should only apply to this check (to preserve alias handling).
347+
if typing_objects.is_never(extra_items):
348+
return True, typing_extensions.NoExtraItems, False
349+
return False, extra_items, readonly
350+
351+
320352
class InvalidSchemaError(Exception):
321353
"""The core schema is invalid."""
322354

@@ -1482,16 +1514,21 @@ def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.Co
14821514
extra_behavior: core_schema.ExtraBehavior = 'ignore'
14831515
extras_schema: CoreSchema | None = None # For 'allow', equivalent to `Any` - no validation performed.
14841516

1485-
# `__closed__` is `None` when not specified (equivalent to `False`):
1486-
is_closed = bool(getattr(typed_dict_cls, '__closed__', False))
1487-
extra_items = getattr(typed_dict_cls, '__extra_items__', typing_extensions.NoExtraItems)
1517+
is_closed, extra_items, extra_items_readonly = _get_typed_dict_extra_items(typed_dict_cls)
14881518
if is_closed:
14891519
extra_behavior = 'forbid'
14901520
extras_schema = None
14911521
elif not typing_objects.is_noextraitems(extra_items):
14921522
extra_behavior = 'allow'
14931523
extras_schema = self.generate_schema(replace_types(extra_items, typevars_map))
14941524

1525+
if extra_items_readonly:
1526+
warnings.warn(
1527+
f'Extra items on TypedDict class {typed_dict_cls.__name__!r} are using the `ReadOnly` qualifier. '
1528+
'Pydantic will not protect items from any mutation on dictionary instances.',
1529+
UserWarning,
1530+
)
1531+
14951532
if (config_extra := self._config_wrapper.extra) in ('allow', 'forbid'):
14961533
if is_closed and config_extra == 'allow':
14971534
warnings.warn(

‎tests/test_types_typeddict.py‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import typing_extensions
1111
from annotated_types import Lt
1212
from pydantic_core import core_schema
13-
from typing_extensions import NotRequired, ReadOnly, Required, TypedDict
13+
from typing_extensions import Never, NotRequired, ReadOnly, Required, TypedDict
1414

1515
from pydantic import (
1616
BaseModel,
@@ -1053,6 +1053,83 @@ class TD(TypedDict, Generic[T], extra_items=T):
10531053
assert exc.value.errors()[0]['loc'] == ('extra',)
10541054

10551055

1056+
def test_typeddict_closed_inherited() -> None:
1057+
"""https://github.com/pydantic/pydantic/issues/13919"""
1058+
1059+
class Parent(TypedDict, closed=True):
1060+
f: int
1061+
1062+
class Child(Parent):
1063+
pass
1064+
1065+
ta = TypeAdapter(Child)
1066+
1067+
with pytest.raises(ValidationError) as exc:
1068+
ta.validate_python({'f': 1, 'extra': 1})
1069+
1070+
assert exc.value.errors()[0]['type'] == 'extra_forbidden'
1071+
assert ta.json_schema()['additionalProperties'] is False
1072+
1073+
1074+
def test_typeddict_extraitems_inherited() -> None:
1075+
"""https://github.com/pydantic/pydantic/issues/13919"""
1076+
1077+
class Parent(TypedDict, extra_items=int):
1078+
f: int
1079+
1080+
class Child(Parent):
1081+
g: NotRequired[int]
1082+
1083+
class GrandChild(Child):
1084+
pass
1085+
1086+
ta = TypeAdapter(GrandChild)
1087+
1088+
assert ta.validate_python({'f': 1, 'g': 1, 'extra': '1'}) == {'f': 1, 'g': 1, 'extra': 1}
1089+
assert ta.json_schema()['additionalProperties'] == {'type': 'integer'}
1090+
1091+
1092+
def test_typeddict_extraitems_overridden() -> None:
1093+
class Parent(TypedDict, extra_items=ReadOnly[float]):
1094+
f: int
1095+
1096+
class Child(Parent, extra_items=int):
1097+
pass
1098+
1099+
assert TypeAdapter(Child).json_schema()['additionalProperties'] == {'type': 'integer'}
1100+
1101+
1102+
def test_typeddict_extraitems_never() -> None:
1103+
class TD(TypedDict, extra_items=Never):
1104+
f: int
1105+
1106+
ta = TypeAdapter(TD)
1107+
1108+
with pytest.raises(ValidationError) as exc:
1109+
ta.validate_python({'f': 1, 'extra': 1})
1110+
1111+
assert exc.value.errors()[0]['type'] == 'extra_forbidden'
1112+
assert ta.json_schema()['additionalProperties'] is False
1113+
1114+
1115+
def test_typeddict_extraitems_readonly() -> None:
1116+
class TD(TypedDict, extra_items=ReadOnly[int]):
1117+
f: int
1118+
1119+
with pytest.warns(UserWarning, match="Extra items on TypedDict class 'TD' are using the `ReadOnly` qualifier"):
1120+
ta = TypeAdapter(TD)
1121+
1122+
assert ta.validate_python({'f': 1, 'extra': '1'}) == {'f': 1, 'extra': 1}
1123+
1124+
1125+
def test_typeddict_extraitems_readonly_never() -> None:
1126+
class TD(TypedDict, extra_items=ReadOnly[Never]):
1127+
f: int
1128+
1129+
# No `ReadOnly` warning, as no extra items are allowed:
1130+
assert TypeAdapter(TD).json_schema()['additionalProperties'] is False
1131+
1132+
10561133
def test_typeddict_incompatible_extra_config_warning() -> None:
10571134
@with_config(extra='allow')
10581135
class TD1(TypedDict, closed=True):

0 commit comments

Comments
 (0)