Skip to content

Commit

Permalink
Allow 'closed=False'; define its behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
  • Loading branch information
PIG208 committed Oct 18, 2024
1 parent 9054142 commit 4167a7c
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions peps/pep-0728.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ extra items are allowed, and their value types must be assignable to the
type expression value.

An application of this is to disallow extra items. We propose to add a
``closed`` class parameter, which only accepts ``True`` as the argument.
It should be a runtime error when ``closed=True`` and ``extra_items`` are used
at the same time.
``closed`` class parameter, which only accepts a literal ``True`` or ``False``
as the argument. It should be a runtime error when ``closed`` and
``extra_items`` are used at the same time.

Different from index signatures, the types of the known items do not need to be
assignable to the ``extra_items`` argument.
Expand Down Expand Up @@ -240,16 +240,27 @@ When ``closed=True`` is set, no extra items are allowed. This is a shorthand for
``extra_items=Never``, because there can't be a value type that is assignable to
:class:`~typing.Never`.

Different from ``total``, only a literal ``True`` is supported as the value of
the ``closed`` argument; ``closed`` is unset by default.
Similar to ``total``, only a literal ``True`` or ``False`` is supported as the
value of the ``closed`` argument; ``closed`` is ``False`` by default, which
preserves the previous TypedDict behavior.

The value of ``closed`` is not inherited through subclassing, but the
implicitly set ``extra_items=Never`` is.
implicitly set ``extra_items=Never`` is. It should be an error to use the
default ``closed=False`` when subclassing a closed TypedDict type::

Setting both ``closed=True`` and ``extra_items`` when defining a TypedDict type
class BaseMovie(TypedDict, closed=True):
name: str

class MovieA(BaseMovie): # Not OK. An explicit 'closed=True' is required
pass

class MovieB(BaseMovie, closed=True): # OK
pass

Setting both ``closed`` and ``extra_items`` when defining a TypedDict type
should always be a runtime error::

class Person(TypedDict, closed=True, extra_items=bool): # Not OK. 'closed=True' and 'extra_items' are incompatible
class Person(TypedDict, closed=False, extra_items=bool): # Not OK. 'closed' and 'extra_items' are incompatible
name: str

As a consequence of ``closed=True`` being equivalent to ``extra_items=Never``.
Expand All @@ -263,17 +274,16 @@ The same rules that apply to ``extra_items=Never`` should also apply to
class MovieClosed(Movie, closed=True): # OK
pass

class MovieNever(Movie, extra_items=Never): # OK
class MovieNever(Movie, extra_items=Never): # Not OK. 'closed=True' is preferred
pass

This will be further discussed in
:ref:`a later section <pep728-inheritance-read-only>`.

When neither ``extra_items`` nor ``closed=True`` is specified, the TypedDict
type is considered non-closed. For such non-closed TypedDict types,
it is assumed that they allow non-required extra items of value type
``ReadOnly[object]`` during inheritance or assignability checks.
This preserves the existing behavior of TypedDict.
is assumed to allow non-required extra items of value type ``ReadOnly[object]``
during inheritance or assignability checks. This preserves the existing behavior
of TypedDict.

Interaction with Totality
-------------------------
Expand Down

0 comments on commit 4167a7c

Please sign in to comment.