@@ -32,9 +32,9 @@ def analyze_member_access(name: str,
32
32
is_operator : bool ,
33
33
builtin_type : Callable [[str ], Instance ],
34
34
not_ready_callback : Callable [[str , Context ], None ],
35
- msg : MessageBuilder ,
35
+ msg : MessageBuilder , * ,
36
+ original_type : Type ,
36
37
override_info : TypeInfo = None ,
37
- original_type : Type = None ,
38
38
chk : 'mypy.checker.TypeChecker' = None ) -> Type :
39
39
"""Return the type of attribute `name` of typ.
40
40
@@ -50,7 +50,6 @@ def analyze_member_access(name: str,
50
50
the fallback type, for example.
51
51
original_type is always the type used in the initial call.
52
52
"""
53
- original_type = original_type or typ
54
53
if isinstance (typ , Instance ):
55
54
if name == '__init__' and not is_super :
56
55
# Accessing __init__ in statically typed code would compromise
@@ -75,7 +74,7 @@ def analyze_member_access(name: str,
75
74
if method .is_property :
76
75
assert isinstance (method , OverloadedFuncDef )
77
76
return analyze_var (name , method .items [0 ].var , typ , info , node , is_lvalue , msg ,
78
- not_ready_callback )
77
+ original_type , not_ready_callback )
79
78
if is_lvalue :
80
79
msg .cant_assign_to_method (node )
81
80
signature = function_type (method , builtin_type ('builtins.function' ))
@@ -108,14 +107,15 @@ def analyze_member_access(name: str,
108
107
msg .disable_type_names += 1
109
108
results = [analyze_member_access (name , subtype , node , is_lvalue , is_super ,
110
109
is_operator , builtin_type , not_ready_callback , msg ,
111
- chk = chk )
110
+ original_type = original_type , chk = chk )
112
111
for subtype in typ .items ]
113
112
msg .disable_type_names -= 1
114
113
return UnionType .make_simplified_union (results )
115
114
elif isinstance (typ , TupleType ):
116
115
# Actually look up from the fallback instance type.
117
116
return analyze_member_access (name , typ .fallback , node , is_lvalue , is_super ,
118
- is_operator , builtin_type , not_ready_callback , msg , chk = chk )
117
+ is_operator , builtin_type , not_ready_callback , msg ,
118
+ original_type = original_type , chk = chk )
119
119
elif isinstance (typ , FunctionLike ) and typ .is_type_obj ():
120
120
# Class attribute.
121
121
# TODO super?
@@ -190,15 +190,14 @@ def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo,
190
190
builtin_type : Callable [[str ], Instance ],
191
191
not_ready_callback : Callable [[str , Context ], None ],
192
192
msg : MessageBuilder ,
193
- original_type : Type = None ,
193
+ original_type : Type ,
194
194
chk : 'mypy.checker.TypeChecker' = None ) -> Type :
195
195
"""Analyse attribute access that does not target a method.
196
196
197
197
This is logically part of analyze_member_access and the arguments are similar.
198
198
199
199
original_type is the type of E in the expression E.var
200
200
"""
201
- original_type = original_type or itype
202
201
# It was not a method. Try looking up a variable.
203
202
v = lookup_member_var_or_accessor (info , name , is_lvalue )
204
203
@@ -207,7 +206,8 @@ def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo,
207
206
# The associated Var node of a decorator contains the type.
208
207
v = vv .var
209
208
if isinstance (v , Var ):
210
- return analyze_var (name , v , itype , info , node , is_lvalue , msg , not_ready_callback )
209
+ return analyze_var (name , v , itype , info , node , is_lvalue , msg ,
210
+ original_type , not_ready_callback )
211
211
elif isinstance (v , FuncDef ):
212
212
assert False , "Did not expect a function"
213
213
elif not v and name not in ['__getattr__' , '__setattr__' ]:
@@ -235,16 +235,14 @@ def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo,
235
235
236
236
237
237
def analyze_var (name : str , var : Var , itype : Instance , info : TypeInfo , node : Context ,
238
- is_lvalue : bool , msg : MessageBuilder ,
239
- not_ready_callback : Callable [[str , Context ], None ],
240
- original_type : Type = None ) -> Type :
238
+ is_lvalue : bool , msg : MessageBuilder , original_type : Type ,
239
+ not_ready_callback : Callable [[str , Context ], None ]) -> Type :
241
240
"""Analyze access to an attribute via a Var node.
242
241
243
242
This is conceptually part of analyze_member_access and the arguments are similar.
244
243
245
244
original_type is the type of E in the expression E.var
246
245
"""
247
- original_type = original_type or itype
248
246
# Found a member variable.
249
247
itype = map_instance_to_supertype (itype , var .info )
250
248
typ = var .type
@@ -300,7 +298,7 @@ def handle_partial_attribute_type(typ: PartialType, is_lvalue: bool, msg: Messag
300
298
301
299
302
300
def lookup_member_var_or_accessor (info : TypeInfo , name : str ,
303
- is_lvalue : bool ) -> SymbolNode :
301
+ is_lvalue : bool ) -> Optional [ SymbolNode ] :
304
302
"""Find the attribute/accessor node that refers to a member of a type."""
305
303
# TODO handle lvalues
306
304
node = info .get (name )
@@ -346,7 +344,7 @@ def analyze_class_attribute_access(itype: Instance,
346
344
builtin_type : Callable [[str ], Instance ],
347
345
not_ready_callback : Callable [[str , Context ], None ],
348
346
msg : MessageBuilder ,
349
- original_type : Type = None ) -> Type :
347
+ original_type : Type ) -> Type :
350
348
"""original_type is the type of E in the expression E.var"""
351
349
node = itype .type .get (name )
352
350
if not node :
@@ -391,7 +389,7 @@ def analyze_class_attribute_access(itype: Instance,
391
389
392
390
def add_class_tvars (t : Type , itype : Instance , is_classmethod : bool ,
393
391
builtin_type : Callable [[str ], Instance ],
394
- original_type : Type = None ) -> Type :
392
+ original_type : Type ) -> Type :
395
393
"""Instantiate type variables during analyze_class_attribute_access,
396
394
e.g T and Q in the following:
397
395
@@ -405,21 +403,19 @@ class B(A): pass
405
403
406
404
original_type is the value of the type B in the expression B.foo()
407
405
"""
408
- # TODO: verify consistency betweem Q and T
406
+ # TODO: verify consistency between Q and T
409
407
info = itype .type # type: TypeInfo
410
408
if isinstance (t , CallableType ):
411
409
# TODO: Should we propagate type variable values?
412
- vars = [TypeVarDef (n , i + 1 , None , builtin_type ('builtins.object' ), tv .variance )
413
- for (i , n ), tv in zip (enumerate (info .type_vars ), info .defn .type_vars )]
410
+ tvars = [TypeVarDef (n , i + 1 , None , builtin_type ('builtins.object' ), tv .variance )
411
+ for (i , n ), tv in zip (enumerate (info .type_vars ), info .defn .type_vars )]
414
412
if is_classmethod :
415
- if not isinstance (original_type , TypeType ):
416
- original_type = TypeType (itype )
417
413
t = bind_self (t , original_type )
418
- return t .copy_modified (variables = vars + t .variables )
414
+ return t .copy_modified (variables = tvars + t .variables )
419
415
elif isinstance (t , Overloaded ):
420
- return Overloaded ([cast (CallableType , add_class_tvars (i , itype , is_classmethod ,
416
+ return Overloaded ([cast (CallableType , add_class_tvars (item , itype , is_classmethod ,
421
417
builtin_type , original_type ))
422
- for i in t .items ()])
418
+ for item in t .items ()])
423
419
return t
424
420
425
421
@@ -573,15 +569,15 @@ class B(A): pass
573
569
return cast (F , func )
574
570
if func .arg_kinds [0 ] == ARG_STAR :
575
571
# The signature is of the form 'def foo(*args, ...)'.
576
- # In this case we shouldn'func drop the first arg,
572
+ # In this case we shouldn't drop the first arg,
577
573
# since func will be absorbed by the *args.
578
574
579
575
# TODO: infer bounds on the type of *args?
580
576
return cast (F , func )
581
577
self_param_type = func .arg_types [0 ]
582
578
if func .variables and (isinstance (self_param_type , TypeVarType ) or
583
- (isinstance (self_param_type , TypeType ) and
584
- isinstance (self_param_type .item , TypeVarType ))):
579
+ (isinstance (self_param_type , TypeType ) and
580
+ isinstance (self_param_type .item , TypeVarType ))):
585
581
if original_type is None :
586
582
# Type check method override
587
583
# XXX value restriction as union?
@@ -601,10 +597,10 @@ def expand(target: Type) -> Type:
601
597
ret_type = func .ret_type
602
598
variables = func .variables
603
599
res = func .copy_modified (arg_types = arg_types ,
604
- arg_kinds = func .arg_kinds [1 :],
605
- arg_names = func .arg_names [1 :],
606
- variables = variables ,
607
- ret_type = ret_type )
600
+ arg_kinds = func .arg_kinds [1 :],
601
+ arg_names = func .arg_names [1 :],
602
+ variables = variables ,
603
+ ret_type = ret_type )
608
604
return cast (F , res )
609
605
610
606
0 commit comments