|
98 | 98 | inspect_field_serializer, |
99 | 99 | inspect_model_serializer, |
100 | 100 | inspect_validator, |
| 101 | + mro, |
101 | 102 | ) |
102 | 103 | from ._docs_extraction import extract_docstrings_from_cls |
103 | 104 | from ._fields import ( |
@@ -317,6 +318,37 @@ def _add_custom_serialization_from_json_encoders( |
317 | 318 | """Errors raised during core schema generation. This does *not* include `InvalidSchemaError`, which is raised during schema cleaning.""" |
318 | 319 |
|
319 | 320 |
|
| 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 | + |
320 | 352 | class InvalidSchemaError(Exception): |
321 | 353 | """The core schema is invalid.""" |
322 | 354 |
|
@@ -1482,16 +1514,21 @@ def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.Co |
1482 | 1514 | extra_behavior: core_schema.ExtraBehavior = 'ignore' |
1483 | 1515 | extras_schema: CoreSchema | None = None # For 'allow', equivalent to `Any` - no validation performed. |
1484 | 1516 |
|
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) |
1488 | 1518 | if is_closed: |
1489 | 1519 | extra_behavior = 'forbid' |
1490 | 1520 | extras_schema = None |
1491 | 1521 | elif not typing_objects.is_noextraitems(extra_items): |
1492 | 1522 | extra_behavior = 'allow' |
1493 | 1523 | extras_schema = self.generate_schema(replace_types(extra_items, typevars_map)) |
1494 | 1524 |
|
| 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 | + |
1495 | 1532 | if (config_extra := self._config_wrapper.extra) in ('allow', 'forbid'): |
1496 | 1533 | if is_closed and config_extra == 'allow': |
1497 | 1534 | warnings.warn( |
|
0 commit comments