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