9494 TypeTranslator , TypeOfAny , TypeType , NoneType , PlaceholderType , TPDICT_NAMES , ProperType ,
9595 get_proper_type , get_proper_types , TypeAliasType , TypeVarLikeType
9696)
97- from mypy .typeops import function_type
97+ from mypy .typeops import function_type , get_type_vars
9898from mypy .type_visitor import TypeQuery
9999from mypy .typeanal import (
100100 TypeAnalyser , analyze_type_alias , no_subscript_builtin_alias ,
@@ -3337,6 +3337,12 @@ def check_classvar(self, s: AssignmentStmt) -> None:
33373337 node = lvalue .node
33383338 if isinstance (node , Var ):
33393339 node .is_classvar = True
3340+ analyzed = self .anal_type (s .type )
3341+ if analyzed is not None and get_type_vars (analyzed ):
3342+ # This means that we have a type var defined inside of a ClassVar.
3343+ # This is not allowed by PEP526.
3344+ # See https://github.com/python/mypy/issues/11538
3345+ self .fail (message_registry .CLASS_VAR_WITH_TYPEVARS , s )
33403346 elif not isinstance (lvalue , MemberExpr ) or self .is_self_member_ref (lvalue ):
33413347 # In case of member access, report error only when assigning to self
33423348 # Other kinds of member assignments should be already reported
@@ -3359,7 +3365,7 @@ def is_final_type(self, typ: Optional[Type]) -> bool:
33593365 return sym .node .fullname in ('typing.Final' , 'typing_extensions.Final' )
33603366
33613367 def fail_invalid_classvar (self , context : Context ) -> None :
3362- self .fail ('ClassVar can only be used for assignments in class body' , context )
3368+ self .fail (message_registry . CLASS_VAR_OUTSIDE_OF_CLASS , context )
33633369
33643370 def process_module_assignment (self , lvals : List [Lvalue ], rval : Expression ,
33653371 ctx : AssignmentStmt ) -> None :
0 commit comments