Skip to content

Conversation

GBeauregard
Copy link
Contributor

@GBeauregard GBeauregard commented Jan 28, 2022

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] to
ClassVar[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.

https://bugs.python.org/issue46553

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] to
ClassVar[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 annotation despite nesting these
forms being generally disallowed. Addressing this limitation could be
future work.
@GBeauregard GBeauregard changed the title typing: allow bare ClassVar annotations bpo-46553: allow bare typing.ClassVar annotations Jan 28, 2022
@bedevere-bot
Copy link

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

@GBeauregard GBeauregard deleted the fix-classvar-bare branch February 6, 2022 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants