@@ -3249,13 +3249,13 @@ def apply_type_arguments_to_callable(
3249
3249
3250
3250
def visit_list_expr (self , e : ListExpr ) -> Type :
3251
3251
"""Type check a list expression [...]."""
3252
- return self .check_lst_expr (e . items , 'builtins.list' , '<list>' , e )
3252
+ return self .check_lst_expr (e , 'builtins.list' , '<list>' )
3253
3253
3254
3254
def visit_set_expr (self , e : SetExpr ) -> Type :
3255
- return self .check_lst_expr (e . items , 'builtins.set' , '<set>' , e )
3255
+ return self .check_lst_expr (e , 'builtins.set' , '<set>' )
3256
3256
3257
3257
def fast_container_type (
3258
- self , items : List [ Expression ], container_fullname : str
3258
+ self , e : Union [ ListExpr , SetExpr , TupleExpr ], container_fullname : str
3259
3259
) -> Optional [Type ]:
3260
3260
"""
3261
3261
Fast path to determine the type of a list or set literal,
@@ -3270,21 +3270,26 @@ def fast_container_type(
3270
3270
ctx = self .type_context [- 1 ]
3271
3271
if ctx :
3272
3272
return None
3273
+ if e ._resolved_type is not None :
3274
+ return e ._resolved_type if isinstance (e ._resolved_type , Instance ) else None
3273
3275
values : List [Type ] = []
3274
- for item in items :
3276
+ for item in e . items :
3275
3277
if isinstance (item , StarExpr ):
3276
3278
# fallback to slow path
3279
+ e ._resolved_type = NoneType ()
3277
3280
return None
3278
3281
values .append (self .accept (item ))
3279
3282
vt = join .join_type_list (values )
3280
3283
if not isinstance (vt , Instance ):
3281
3284
return None
3282
- return self .chk .named_generic_type (container_fullname , [vt ])
3285
+ ct = self .chk .named_generic_type (container_fullname , [vt ])
3286
+ e ._resolved_type = ct
3287
+ return ct
3283
3288
3284
- def check_lst_expr (self , items : List [ Expression ], fullname : str ,
3285
- tag : str , context : Context ) -> Type :
3289
+ def check_lst_expr (self , e : Union [ ListExpr , SetExpr , TupleExpr ], fullname : str ,
3290
+ tag : str ) -> Type :
3286
3291
# fast path
3287
- t = self .fast_container_type (items , fullname )
3292
+ t = self .fast_container_type (e , fullname )
3288
3293
if t :
3289
3294
return t
3290
3295
@@ -3303,10 +3308,10 @@ def check_lst_expr(self, items: List[Expression], fullname: str,
3303
3308
variables = [tv ])
3304
3309
out = self .check_call (constructor ,
3305
3310
[(i .expr if isinstance (i , StarExpr ) else i )
3306
- for i in items ],
3311
+ for i in e . items ],
3307
3312
[(nodes .ARG_STAR if isinstance (i , StarExpr ) else nodes .ARG_POS )
3308
- for i in items ],
3309
- context )[0 ]
3313
+ for i in e . items ],
3314
+ e )[0 ]
3310
3315
return remove_instance_last_known_values (out )
3311
3316
3312
3317
def visit_tuple_expr (self , e : TupleExpr ) -> Type :
@@ -3356,7 +3361,7 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type:
3356
3361
else :
3357
3362
# A star expression that's not a Tuple.
3358
3363
# Treat the whole thing as a variable-length tuple.
3359
- return self .check_lst_expr (e . items , 'builtins.tuple' , '<tuple>' , e )
3364
+ return self .check_lst_expr (e , 'builtins.tuple' , '<tuple>' )
3360
3365
else :
3361
3366
if not type_context_items or j >= len (type_context_items ):
3362
3367
tt = self .accept (item )
@@ -3382,6 +3387,8 @@ def fast_dict_type(self, e: DictExpr) -> Optional[Type]:
3382
3387
ctx = self .type_context [- 1 ]
3383
3388
if ctx :
3384
3389
return None
3390
+ if e ._resolved_type is not None :
3391
+ return e ._resolved_type if isinstance (e ._resolved_type , Instance ) else None
3385
3392
keys : List [Type ] = []
3386
3393
values : List [Type ] = []
3387
3394
stargs : Optional [Tuple [Type , Type ]] = None
@@ -3395,17 +3402,22 @@ def fast_dict_type(self, e: DictExpr) -> Optional[Type]:
3395
3402
):
3396
3403
stargs = (st .args [0 ], st .args [1 ])
3397
3404
else :
3405
+ e ._resolved_type = NoneType ()
3398
3406
return None
3399
3407
else :
3400
3408
keys .append (self .accept (key ))
3401
3409
values .append (self .accept (value ))
3402
3410
kt = join .join_type_list (keys )
3403
3411
vt = join .join_type_list (values )
3404
3412
if not (isinstance (kt , Instance ) and isinstance (vt , Instance )):
3413
+ e ._resolved_type = NoneType ()
3405
3414
return None
3406
3415
if stargs and (stargs [0 ] != kt or stargs [1 ] != vt ):
3416
+ e ._resolved_type = NoneType ()
3407
3417
return None
3408
- return self .chk .named_generic_type ('builtins.dict' , [kt , vt ])
3418
+ dt = self .chk .named_generic_type ('builtins.dict' , [kt , vt ])
3419
+ e ._resolved_type = dt
3420
+ return dt
3409
3421
3410
3422
def visit_dict_expr (self , e : DictExpr ) -> Type :
3411
3423
"""Type check a dict expression.
0 commit comments