Skip to content

Now ClassVar cannot contain type variables, refs #11538 #11585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
'Cannot override class variable (previously declared on base class "{}") with instance '
"variable"
)
CLASS_VAR_WITH_TYPEVARS: Final = 'ClassVar cannot contain type variables'
CLASS_VAR_OUTSIDE_OF_CLASS: Final = (
'ClassVar can only be used for assignments in class body'
)

# Protocol
RUNTIME_PROTOCOL_EXPECTED: Final = ErrorMessage(
Expand Down
10 changes: 8 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType
)
from mypy.typeops import function_type
from mypy.typeops import function_type, get_type_vars
from mypy.type_visitor import TypeQuery
from mypy.typeanal import (
TypeAnalyser, analyze_type_alias, no_subscript_builtin_alias,
Expand Down Expand Up @@ -3337,6 +3337,12 @@ def check_classvar(self, s: AssignmentStmt) -> None:
node = lvalue.node
if isinstance(node, Var):
node.is_classvar = True
analyzed = self.anal_type(s.type)
if analyzed is not None and get_type_vars(analyzed):
# This means that we have a type var defined inside of a ClassVar.
# This is not allowed by PEP526.
# See https://github.com/python/mypy/issues/11538
self.fail(message_registry.CLASS_VAR_WITH_TYPEVARS, s)
elif not isinstance(lvalue, MemberExpr) or self.is_self_member_ref(lvalue):
# In case of member access, report error only when assigning to self
# Other kinds of member assignments should be already reported
Expand All @@ -3359,7 +3365,7 @@ def is_final_type(self, typ: Optional[Type]) -> bool:
return sym.node.fullname in ('typing.Final', 'typing_extensions.Final')

def fail_invalid_classvar(self, context: Context) -> None:
self.fail('ClassVar can only be used for assignments in class body', context)
self.fail(message_registry.CLASS_VAR_OUTSIDE_OF_CLASS, context)

def process_module_assignment(self, lvals: List[Lvalue], rval: Expression,
ctx: AssignmentStmt) -> None:
Expand Down
30 changes: 16 additions & 14 deletions mypy/test/testsemanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
# Semantic analyzer test cases: dump parse tree

# Semantic analysis test case description files.
semanal_files = ['semanal-basic.test',
'semanal-expressions.test',
'semanal-classes.test',
'semanal-types.test',
'semanal-typealiases.test',
'semanal-modules.test',
'semanal-statements.test',
'semanal-abstractclasses.test',
'semanal-namedtuple.test',
'semanal-typeddict.test',
'semenal-literal.test',
'semanal-classvar.test',
'semanal-python2.test',
'semanal-lambda.test']
semanal_files = [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is important to me, because I use cmd + # to comment out all the files and uncomment only a single one I am interested in.

With the previous format it is impossible to do.

Btw, is there any more useful way to run all tests in a file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest ./mypy/test/testsemanal.py works well for me. pytest -k search_phrase is also really useful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but pytest ./mypy/test/testsemanal.py will run all semanal-*.test files.
I am asking about a single .test file, for example only semanal-classvar.test

'semanal-basic.test',
'semanal-expressions.test',
'semanal-classes.test',
'semanal-types.test',
'semanal-typealiases.test',
'semanal-modules.test',
'semanal-statements.test',
'semanal-abstractclasses.test',
'semanal-namedtuple.test',
'semanal-typeddict.test',
'semenal-literal.test',
'semanal-classvar.test',
'semanal-python2.test',
'semanal-lambda.test',
]


def get_semanal_options(program_text: str, testcase: DataDrivenTestCase) -> Options:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classvar.test
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ main:3: error: Cannot assign to class variable "x" via instance
from typing import ClassVar, Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
x: ClassVar[T]
x: ClassVar[T] # E: ClassVar cannot contain type variables
@classmethod
def foo(cls) -> T:
return cls.x # OK
Expand All @@ -308,7 +308,7 @@ from typing import ClassVar, Generic, Tuple, TypeVar, Union, Type
T = TypeVar('T')
U = TypeVar('U')
class A(Generic[T, U]):
x: ClassVar[Union[T, Tuple[U, Type[U]]]]
x: ClassVar[Union[T, Tuple[U, Type[U]]]] # E: ClassVar cannot contain type variables
@classmethod
def foo(cls) -> Union[T, Tuple[U, Type[U]]]:
return cls.x # OK
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/semanal-classvar.test
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,14 @@ class B:
pass
[out]
main:4: error: ClassVar can only be used for assignments in class body

[case testClassVarWithTypeVariable]
from typing import ClassVar, TypeVar, Generic, List

T = TypeVar('T')

class Some(Generic[T]):
error: ClassVar[T] # E: ClassVar cannot contain type variables
nested: ClassVar[List[List[T]]] # E: ClassVar cannot contain type variables
ok: ClassVar[int]
[out]