diff --git a/peps/pep-0728.rst b/peps/pep-0728.rst index 790d57510b3..4880e0bd360 100644 --- a/peps/pep-0728.rst +++ b/peps/pep-0728.rst @@ -240,7 +240,12 @@ The ``closed`` Class Parameter ------------------------------ When neither ``extra_items`` nor ``closed=True`` is specified, ``closed=False`` -is assumed. +is assumed. The TypedDict should allow non-required extra items of value type +``ReadOnly[object]`` during inheritance or assignability checks, to +preserve the default TypedDict behavior. Extra keys included in TypedDict +object construction should still be caught, as mentioned in TypedDict's +`typing spec +`__. When ``closed=True`` is set, no extra items are allowed. This is equivalent to ``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:: This will be further discussed in :ref:`a later section `. -The TypedDict should allow non-required extra items of value type -``ReadOnly[object]`` during inheritance or assignability checks, to -preserve the default TypedDict behavior. Extra keys included in TypedDict -object construction should still be caught, as mentioned in TypedDict's -`typing spec -`__. - ``closed`` is also supported with the functional syntax:: Movie = TypedDict("Movie", {"name": str}, closed=True) @@ -331,13 +329,21 @@ For type checking purposes, ``Unpack[SomeTypedDict]`` with extra items should be treated as its equivalent in regular parameters, and the existing rules for function parameters still apply:: - class Movie(TypedDict, extra_items=int): + class MovieNoExtra(TypedDict): name: str - def f(**kwargs: Unpack[Movie]) -> None: ... + class MovieExtra(TypedDict, extra_items=int): + name: str + + def f(**kwargs: Unpack[MovieNoExtra]) -> None: ... + def g(**kwargs: Unpack[MovieExtra]) -> None: ... # Should be equivalent to: - def f(*, name: str, **kwargs: int) -> None: ... + def f(*, name: str) -> None: ... + def g(*, name: str, **kwargs: int) -> None: ... + + f(name="No Country for Old Men", year=2007) # Not OK. Unrecognized item + g(name="No Country for Old Men", year=2007) # OK Interaction with Read-only Items --------------------------------