Skip to content

Commit 0f18c4b

Browse files
committed
feedback
1 parent eb914a4 commit 0f18c4b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

docs/spec/glossary.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This section defines a few terms that may be used elsewhere in the specification
3131
A :ref:`TypedDict <typeddict>` type is closed if it may not contain any
3232
additional :term:`items <item>` beyond those specified in the TypedDict definition.
3333
A closed TypedDict can be created using the ``closed=True`` argument to
34-
:py:func:`typing.TypedDict`.
34+
:py:func:`typing.TypedDict`, or equivalently by setting ``extra_items=Never``.
3535
Compare :term:`extra items` and :term:`open`.
3636

3737
consistent
@@ -100,10 +100,9 @@ This section defines a few terms that may be used elsewhere in the specification
100100
:pep:`3107` syntax (the filename ends in ``.py``).
101101

102102
item
103-
In the context of a :ref:`TypedDict <typeddict>`, an item is a key/value
104-
pair defined in the TypedDict definition. Each item has a name (the key)
105-
and a type (the value). Items may be :term:`required` or
106-
:term:`non-required`, and may be :term:`read-only` or writable.
103+
In the context of a :ref:`TypedDict <typeddict>`, an item consists of a name
104+
(the dictionary key) and a type (representing the type that values corresponding to the key must have).
105+
Items may be :term:`required` or :term:`non-required`, and may be :term:`read-only` or writable.
107106

108107
materialize
109108
A :term:`gradual type` can be materialized to a more static type

docs/spec/typeddict.rst

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ TypedDict types can define any number of :term:`items <item>`, which are string
1717
keys associated with values of a specified type. For example,
1818
a TypedDict may contain the item ``a: str``, indicating that the key ``a``
1919
must map to a value of type ``str``. Items may be either :term:`required`,
20-
meaning they must be present in any instance of the TypedDict type, or
20+
meaning they must be present in every instance of the TypedDict type, or
2121
:term:`non-required`, meaning they may be omitted, but if they are present,
2222
they must be of the type specified in the TypedDict definition. By default,
2323
all items in a TypedDict are mutable, but items
@@ -395,14 +395,19 @@ is inherited from its superclass by default::
395395
pass
396396

397397
However, subclasses may also explicitly use the ``closed`` and ``extra_items`` arguments
398-
to change the openness of the TypedDict, but in some cases this yields a type checker error.
399-
If the base class is open, all possible states are allowed in the subclass: it may remain open,
400-
it may be closed (with ``closed=True``), or it may have extra items (with ``extra_items=...``).
401-
If the base class is closed, any child classes must also be closed.
402-
If the base class has extra items, but they are not read-only, the child class must also allow
403-
the same extra items. If the base class has read-only extra items, the child class may be closed,
404-
or it may redeclare its extra items with a type that is :term:`assignable` to the base class type.
405-
Child classes may also have mutable extra items if the base class has read-only extra items.
398+
to change the openness of the TypedDict, but in some cases this yields a type checker error:
399+
400+
- If the base class is open, all possible states are allowed in the subclass: it may remain open,
401+
it may be closed (with ``closed=True``), or it may have extra items (with ``extra_items=...``).
402+
403+
- If the base class is closed, any child classes must also be closed.
404+
405+
- If the base class has extra items, but they are not read-only, the child class must also allow
406+
the same extra items.
407+
408+
- If the base class has read-only extra items, the child class may be closed,
409+
or it may redeclare its extra items with a type that is :term:`assignable` to the base class type.
410+
Child classes may also have mutable extra items if the base class has read-only extra items.
406411

407412
For example::
408413

@@ -607,6 +612,7 @@ A TypedDict type is a subtype of ``dict[str, VT]`` if the following conditions a
607612

608613
- The TypedDict type has mutable :term:`extra items` of a type that is :term:`equivalent` to ``VT``.
609614
- All items on the TypedDict satisfy the following conditions:
615+
610616
- The value type of the item is :term:`equivalent` to ``VT``.
611617
- The item is not read-only.
612618
- The item is not required.
@@ -627,7 +633,7 @@ For example::
627633
regular_dict: dict[str, int] = not_required_num_dict # OK
628634
f(not_required_num_dict) # OK
629635

630-
In this case, methods that are previously unavailable on a TypedDict are allowed,
636+
In this case, some methods that are otherwise unavailable on a TypedDict are allowed,
631637
with signatures matching ``dict[str, VT]``
632638
(e.g.: ``__setitem__(self, key: str, value: VT) -> None``)::
633639

@@ -742,15 +748,15 @@ This section discusses some specific operations in more detail.
742748
if the string value of ``e`` cannot be determined statically.
743749
(This simplifies to ``object`` if ``d`` is :term:`open`.)
744750

745-
* ``clear()`` is not safe on :term:`open` TypedDicts since it could remove required keys, some of which
751+
* ``clear()`` is not safe on :term:`open` TypedDicts since it could remove required items, some of which
746752
may not be directly visible because of :term:`structural`
747753
:term:`assignability <assignable>`. However, this method is safe on
748754
:term:`closed` TypedDicts and TypedDicts with :term:`extra items` if
749755
there are no required or read-only items and there cannot be any subclasses with required
750756
or read-only items.
751757

752758
* ``popitem()`` is similarly unsafe on many TypedDicts, even
753-
if all known keys are not required (``total=False``).
759+
if all known items are :term:`non-required`.
754760

755761
* ``del obj['key']`` should be rejected unless ``'key'`` is a
756762
non-required, mutable key.
@@ -785,14 +791,14 @@ This section discusses some specific operations in more detail.
785791
* The ``update()`` method should not allow mutating a read-only item.
786792
Therefore, type checkers should error if a
787793
TypedDict with a read-only item is updated with another TypedDict that declares
788-
that key::
794+
that item::
789795

790796
class A(TypedDict):
791797
x: ReadOnly[int]
792798
y: int
793799

794-
a1: A = { "x": 1, "y": 2 }
795-
a2: A = { "x": 3, "y": 4 }
800+
a1: A = {"x": 1, "y": 2}
801+
a2: A = {"x": 3, "y": 4}
796802
a1.update(a2) # Type check error: "x" is read-only in A
797803

798804
Unless the declared value is of bottom type (:data:`~typing.Never`)::

0 commit comments

Comments
 (0)