Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions peps/pep-0728.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://typing.python.org/en/latest/spec/typeddict.html#supported-and-unsupported-operations.>`__.

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
Expand Down Expand Up @@ -293,13 +298,6 @@ argument is a read-only type::
This will be further discussed in
:ref:`a later section <pep728-inheritance-read-only>`.

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
<https://typing.python.org/en/latest/spec/typeddict.html#supported-and-unsupported-operations.>`__.

``closed`` is also supported with the functional syntax::

Movie = TypedDict("Movie", {"name": str}, closed=True)
Expand Down Expand Up @@ -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
--------------------------------
Expand Down