@@ -240,7 +240,12 @@ The ``closed`` Class Parameter
240
240
------------------------------
241
241
242
242
When neither ``extra_items `` nor ``closed=True `` is specified, ``closed=False ``
243
- is assumed.
243
+ is assumed. The TypedDict should allow non-required extra items of value type
244
+ ``ReadOnly[object] `` during inheritance or assignability checks, to
245
+ preserve the default TypedDict behavior. Extra keys included in TypedDict
246
+ object construction should still be caught, as mentioned in TypedDict's
247
+ `typing spec
248
+ <https://typing.python.org/en/latest/spec/typeddict.html#supported-and-unsupported-operations.> `__.
244
249
245
250
When ``closed=True `` is set, no extra items are allowed. This is equivalent to
246
251
``extra_items=Never ``, because there can't be a value type that is assignable to
@@ -293,13 +298,6 @@ argument is a read-only type::
293
298
This will be further discussed in
294
299
:ref: `a later section <pep728-inheritance-read-only >`.
295
300
296
- The TypedDict should allow non-required extra items of value type
297
- ``ReadOnly[object] `` during inheritance or assignability checks, to
298
- preserve the default TypedDict behavior. Extra keys included in TypedDict
299
- object construction should still be caught, as mentioned in TypedDict's
300
- `typing spec
301
- <https://typing.python.org/en/latest/spec/typeddict.html#supported-and-unsupported-operations.> `__.
302
-
303
301
``closed `` is also supported with the functional syntax::
304
302
305
303
Movie = TypedDict("Movie", {"name": str}, closed=True)
@@ -331,13 +329,21 @@ For type checking purposes, ``Unpack[SomeTypedDict]`` with extra items should be
331
329
treated as its equivalent in regular parameters, and the existing rules for
332
330
function parameters still apply::
333
331
334
- class Movie (TypedDict, extra_items=int ):
332
+ class MovieNoExtra (TypedDict):
335
333
name: str
336
334
337
- def f(**kwargs: Unpack[Movie]) -> None: ...
335
+ class MovieExtra(TypedDict, extra_items=int):
336
+ name: str
337
+
338
+ def f(**kwargs: Unpack[MovieNoExtra]) -> None: ...
339
+ def g(**kwargs: Unpack[MovieExtra]) -> None: ...
338
340
339
341
# Should be equivalent to:
340
- def f(*, name: str, **kwargs: int) -> None: ...
342
+ def f(*, name: str) -> None: ...
343
+ def g(*, name: str, **kwargs: int) -> None: ...
344
+
345
+ f(name="No Country for Old Men", year=2007) # Not OK. Unrecognized item
346
+ g(name="No Country for Old Men", year=2007) # OK
341
347
342
348
Interaction with Read-only Items
343
349
--------------------------------
0 commit comments