bpo-46553: allow bare typing.ClassVar annotations by GBeauregard · Pull Request #30983 · python/cpython (original) (raw)
This is a simple matter of just adding ClassVar
to the list of types allowed to be bare before the later call that raises for bare types.
These are used in the wild and covered by dataclasses unit tests.
Several static type checkers support this pattern.
Note we adjust one test from ClassVar[ClassVar]
toClassVar[ClassVar[int]]
so that it still fails. This is surprising, but
not a result of weirdness we're doing in this patch. This is a
limitation of the current way runtime type checking works that this case
is allowed when bare special forms are allowed. For example, before this
patch ClassVar[Final]
is a valid runtime annotation despite nesting these
forms being generally disallowed, so it's expected allowing bare ClassVar
makes ClassVar[ClassVar]
valid as well.