@@ -17,7 +17,7 @@ TypedDict types can define any number of :term:`items <item>`, which are string
17
17
keys associated with values of a specified type. For example,
18
18
a TypedDict may contain the item ``a: str ``, indicating that the key ``a ``
19
19
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
21
21
:term: `non-required `, meaning they may be omitted, but if they are present,
22
22
they must be of the type specified in the TypedDict definition. By default,
23
23
all items in a TypedDict are mutable, but items
@@ -395,14 +395,19 @@ is inherited from its superclass by default::
395
395
pass
396
396
397
397
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.
406
411
407
412
For example::
408
413
@@ -607,6 +612,7 @@ A TypedDict type is a subtype of ``dict[str, VT]`` if the following conditions a
607
612
608
613
- The TypedDict type has mutable :term: `extra items ` of a type that is :term: `equivalent ` to ``VT ``.
609
614
- All items on the TypedDict satisfy the following conditions:
615
+
610
616
- The value type of the item is :term: `equivalent ` to ``VT ``.
611
617
- The item is not read-only.
612
618
- The item is not required.
@@ -627,7 +633,7 @@ For example::
627
633
regular_dict: dict[str, int] = not_required_num_dict # OK
628
634
f(not_required_num_dict) # OK
629
635
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,
631
637
with signatures matching ``dict[str, VT] ``
632
638
(e.g.: ``__setitem__(self, key: str, value: VT) -> None ``)::
633
639
@@ -742,15 +748,15 @@ This section discusses some specific operations in more detail.
742
748
if the string value of ``e `` cannot be determined statically.
743
749
(This simplifies to ``object `` if ``d `` is :term: `open `.)
744
750
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
746
752
may not be directly visible because of :term: `structural `
747
753
:term: `assignability <assignable> `. However, this method is safe on
748
754
:term: `closed ` TypedDicts and TypedDicts with :term: `extra items ` if
749
755
there are no required or read-only items and there cannot be any subclasses with required
750
756
or read-only items.
751
757
752
758
* ``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` .
754
760
755
761
* ``del obj['key'] `` should be rejected unless ``'key' `` is a
756
762
non-required, mutable key.
@@ -785,14 +791,14 @@ This section discusses some specific operations in more detail.
785
791
* The ``update() `` method should not allow mutating a read-only item.
786
792
Therefore, type checkers should error if a
787
793
TypedDict with a read-only item is updated with another TypedDict that declares
788
- that key ::
794
+ that item ::
789
795
790
796
class A(TypedDict):
791
797
x: ReadOnly[int]
792
798
y: int
793
799
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}
796
802
a1.update(a2) # Type check error: "x" is read-only in A
797
803
798
804
Unless the declared value is of bottom type (:data: `~typing.Never `)::
0 commit comments