There are a couple things that I find confusing in this PEP:
-
The motivation section states that a TypedDict can have extra items that are not visible through its type. The way I naively understand this sentence is that the following should be allowed:
class TD(TypedDict): a: int td: TD = {'a': 1, 'extra': True}
By reading this section of the spec, you better understand the assignability rules, so probably the PEP section should link to this part of the spec. Just to be sure I understand correctly, the fact that the above snippet raises an error is only because type checkers special case typed dictionaries assignments? Because with normal classes, this works:
class A: pass class B(A): pass a: A = B()
Alternatively, the PEP could emphasize on the fact that closed typed dictionaries are only relevant in a context where assignability of arguments to parameters is involved (as “simple” assignments (
a: <typ> = value
) seems to be special cased as I mention above), e.g.:class TD1(TypedDict): a: int class TD2(TypedDict): a: int b: str def func(a: TD1): pass td2: TD2 = ... func(td2) # OK
-
It is stated that
closed=True
is equivalent toextra_items=Never
. Does this mean thatclosed=False
(the default) is equivalent toextra_items=object
? This currently not the case (playground):class TD(TypedDict): a: int class TDExtra(TypedDict, extra_items=object): a: int td1: TD = {'a': 1, 'extra': True} # Error td2: TDExtra = {'a': 1, 'extra': True} # OK
-
One could think that
@final
can be used to mark aTypedDict
as closed. It might be worth mentioning why this is not the case (does not work with the functional syntax, and as the spec example shows, you don’t need to explicitly create subclasses to have the assignability rules applied).