66
77from mypy import meet , message_registry , subtypes
88from mypy .erasetype import erase_typevars
9- from mypy .expandtype import expand_type_by_instance , freshen_function_type_vars
9+ from mypy .expandtype import expand_self_type , expand_type_by_instance , freshen_function_type_vars
1010from mypy .maptype import map_instance_to_supertype
1111from mypy .messages import MessageBuilder
1212from mypy .nodes import (
3737 erase_to_bound ,
3838 function_type ,
3939 make_simplified_union ,
40+ supported_self_type ,
4041 tuple_fallback ,
4142 type_object_type_from_function ,
4243)
@@ -90,6 +91,7 @@ def __init__(
9091 self_type : Type | None ,
9192 module_symbol_table : SymbolTable | None = None ,
9293 no_deferral : bool = False ,
94+ is_self : bool = False ,
9395 ) -> None :
9496 self .is_lvalue = is_lvalue
9597 self .is_super = is_super
@@ -101,6 +103,7 @@ def __init__(
101103 self .chk = chk
102104 self .module_symbol_table = module_symbol_table
103105 self .no_deferral = no_deferral
106+ self .is_self = is_self
104107
105108 def named_type (self , name : str ) -> Instance :
106109 return self .chk .named_type (name )
@@ -152,6 +155,7 @@ def analyze_member_access(
152155 self_type : Type | None = None ,
153156 module_symbol_table : SymbolTable | None = None ,
154157 no_deferral : bool = False ,
158+ is_self : bool = False ,
155159) -> Type :
156160 """Return the type of attribute 'name' of 'typ'.
157161
@@ -187,6 +191,7 @@ def analyze_member_access(
187191 self_type = self_type ,
188192 module_symbol_table = module_symbol_table ,
189193 no_deferral = no_deferral ,
194+ is_self = is_self ,
190195 )
191196 result = _analyze_member_access (name , typ , mx , override_info )
192197 possible_literal = get_proper_type (result )
@@ -682,12 +687,12 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
682687 return inferred_dunder_get_type .ret_type
683688
684689
685- def is_instance_var (var : Var , info : TypeInfo ) -> bool :
690+ def is_instance_var (var : Var ) -> bool :
686691 """Return if var is an instance variable according to PEP 526."""
687692 return (
688693 # check the type_info node is the var (not a decorated function, etc.)
689- var .name in info .names
690- and info .names [var .name ].node is var
694+ var .name in var . info .names
695+ and var . info .names [var .name ].node is var
691696 and not var .is_classvar
692697 # variables without annotations are treated as classvar
693698 and not var .is_inferred
@@ -722,12 +727,16 @@ def analyze_var(
722727 mx .msg .read_only_property (name , itype .type , mx .context )
723728 if mx .is_lvalue and var .is_classvar :
724729 mx .msg .cant_assign_to_classvar (name , mx .context )
730+ if not (mx .is_self or mx .is_super ) or supported_self_type (
731+ get_proper_type (mx .original_type )
732+ ):
733+ typ = expand_self_type (var , typ , mx .original_type )
725734 t = get_proper_type (expand_type_by_instance (typ , itype ))
726735 result : Type = t
727736 typ = get_proper_type (typ )
728737 if (
729738 var .is_initialized_in_class
730- and (not is_instance_var (var , info ) or mx .is_operator )
739+ and (not is_instance_var (var ) or mx .is_operator )
731740 and isinstance (typ , FunctionLike )
732741 and not typ .is_type_obj ()
733742 ):
@@ -945,7 +954,12 @@ def analyze_class_attribute_access(
945954 # x: T
946955 # C.x # Error, ambiguous access
947956 # C[int].x # Also an error, since C[int] is same as C at runtime
948- if isinstance (t , TypeVarType ) or has_type_vars (t ):
957+ # Exception is Self type wrapped in ClassVar, that is safe.
958+ if node .node .info .self_type is not None and node .node .is_classvar :
959+ exclude = node .node .info .self_type .id
960+ else :
961+ exclude = None
962+ if isinstance (t , TypeVarType ) and t .id != exclude or has_type_vars (t , exclude ):
949963 # Exception: access on Type[...], including first argument of class methods is OK.
950964 if not isinstance (get_proper_type (mx .original_type ), TypeType ) or node .implicit :
951965 if node .node .is_classvar :
@@ -958,6 +972,7 @@ def analyze_class_attribute_access(
958972 # In the above example this means that we infer following types:
959973 # C.x -> Any
960974 # C[int].x -> int
975+ t = get_proper_type (expand_self_type (node .node , t , itype ))
961976 t = erase_typevars (expand_type_by_instance (t , isuper ))
962977
963978 is_classmethod = (is_decorated and cast (Decorator , node .node ).func .is_class ) or (
0 commit comments