Consider the following snippet:
(playgrounds: Pyright, Mypy, Pyre)
from typing import ClassVar, TypedDict
class TD(TypedDict):
v: ClassVar[int]
TD(a = 0) # pyright => error: No overloads for "__init__" match the provided arguments
# mypy => error: Missing key "v" for TypedDict "TD"
# pyre => error: Unexpected keyword argument `a` to call `TD.__init__`.
# pytype => error: Expected: (*, v); Actually passed: (a)
TD(v = 0) # pyright => fine
# mypy => fine
# pyre => fine
# pytype => error: Expected: (*, v: ClassVar[int]); Actually passed: (v: int)
TD() # pyright => error: No overloads for "__init__" match the provided arguments
# mypy => error: Missing key "v" for TypedDict "TD"
# pyre => error: Call `TD.__init__` expects argument `v`.
# pytype => error: Expected: (*, v); Actually passed: ()
Pyright, Mypy and Pyre consider v
a required keyword argument, while Pytype fails all calls. Yet none of them thinks the declaration of v
is a problem.
The specification’s ClassVar
and TypedDict
chapters currently don’t say anything about their interactions.
Should ClassVar
be allowed within the body of a TypedDict
?