8000 PEP 728: Address additional feedback (#4383) · python/peps@424872d · GitHub
[go: up one dir, main page]

Skip to content

Commit 424872d

Browse files
authored
PEP 728: Address additional feedback (#4383)
Address feedback from Eric. https://discuss.python.org/t/pep-728-typeddict-with-typed-extra-items/45443/150
1 parent a8a31fe commit 424872d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

peps/pep-0728.rst

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ The ``closed`` Class Parameter
240240
------------------------------
241241

242242
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.>`__.
244249

245250
When ``closed=True`` is set, no extra items are allowed. This is equivalent to
246251
``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::
293298
This will be further discussed in
294299
:ref:`a later section <pep728-inheritance-read-only>`.
295300

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-
303301
``closed`` is also supported with the functional syntax::
304302

305303
Movie = TypedDict("Movie", {"name": str}, closed=True)
@@ -331,13 +329,21 @@ For type checking purposes, ``Unpack[SomeTypedDict]`` with extra items should be
331329
treated as its equivalent in regular parameters, and the existing rules for
332330
function parameters still apply::
333331

334-
class Movie(TypedDict, extra_items=int):
332+
class MovieNoExtra(TypedDict):
335333
name: str
336334

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: ...
338340

339341
# 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
341347

342348
Interaction with Read-only Items
343349
--------------------------------

0 commit comments

Comments
 (0)
0