@@ -3259,13 +3259,13 @@ def apply_type_arguments_to_callable(
3259
3259
3260
3260
def visit_list_expr (self , e : ListExpr ) -> Type :
3261
3261
"""Type check a list expression [...]."""
3262
- return self .check_lst_expr (e . items , 'builtins.list' , '<list>' , e )
3262
+ return self .check_lst_expr (e , 'builtins.list' , '<list>' )
3263
3263
3264
3264
def visit_set_expr (self , e : SetExpr ) -> Type :
3265
- return self .check_lst_expr (e . items , 'builtins.set' , '<set>' , e )
3265
+ return self .check_lst_expr (e , 'builtins.set' , '<set>' )
3266
3266
3267
3267
def fast_container_type (
3268
- self , items : List [ Expression ], container_fullname : str
3268
+ self , e : Union [ ListExpr , SetExpr , TupleExpr ], container_fullname : str
3269
3269
) -> Optional [Type ]:
3270
3270
"""
3271
3271
Fast path to determine the type of a list or set literal,
@@ -3280,21 +3280,27 @@ def fast_container_type(
3280
3280
ctx = self .type_context [- 1 ]
3281
3281
if ctx :
3282
3282
return None
3283
+ if e ._resolved_type is not None :
3284
+ return e ._resolved_type if isinstance (e ._resolved_type , Instance ) else None
3283
3285
values : List [Type ] = []
3284
- for item in items :
3286
+ for item in e . items :
3285
3287
if isinstance (item , StarExpr ):
3286
3288
# fallback to slow path
3289
+ e ._resolved_type = NoneType ()
3287
3290
return None
3288
3291
values .append (self .accept (item ))
3289
3292
vt = join .join_type_list (values )
3290
3293
if not allow_fast_container_literal (vt ):
3294
+ e ._resolved_type = NoneType ()
3291
3295
return None
3292
- return self .chk .named_generic_type (container_fullname , [vt ])
3296
+ ct = self .chk .named_generic_type (container_fullname , [vt ])
3297
+ e ._resolved_type = ct
3298
+ return ct
3293
3299
3294
- def check_lst_expr (self , items : List [ Expression ], fullname : str ,
3295
- tag : str , context : Context ) -> Type :
3300
+ def check_lst_expr (self , e : Union [ ListExpr , SetExpr , TupleExpr ], fullname : str ,
3301
+ tag : str ) -> Type :
3296
3302
# fast path
3297
- t = self .fast_container_type (items , fullname )
3303
+ t = self .fast_container_type (e , fullname )
3298
3304
if t :
3299
3305
return t
3300
3306
@@ -3313,10 +3319,10 @@ def check_lst_expr(self, items: List[Expression], fullname: str,
3313
3319
variables = [tv ])
3314
3320
out = self .check_call (constructor ,
3315
3321
[(i .expr if isinstance (i , StarExpr ) else i )
3316
- for i in items ],
3322
+ for i in e . items ],
3317
3323
[(nodes .ARG_STAR if isinstance (i , StarExpr ) else nodes .ARG_POS )
3318
- for i in items ],
3319
- context )[0 ]
3324
+ for i in e . items ],
3325
+ e )[0 ]
3320
3326
return remove_instance_last_known_values (out )
3321
3327
3322
3328
def visit_tuple_expr (self , e : TupleExpr ) -> Type :
@@ -3366,7 +3372,7 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type:
3366
3372
else :
3367
3373
# A star expression that's not a Tuple.
3368
3374
# Treat the whole thing as a variable-length tuple.
3369
- return self .check_lst_expr (e . items , 'builtins.tuple' , '<tuple>' , e )
3375
+ return self .check_lst_expr (e , 'builtins.tuple' , '<tuple>' )
3370
3376
else :
3371
3377
if not type_context_items or j >= len (type_context_items ):
3372
3378
tt = self .accept (item )
@@ -3392,6 +3398,8 @@ def fast_dict_type(self, e: DictExpr) -> Optional[Type]:
3392
3398
ctx = self .type_context [- 1 ]
3393
3399
if ctx :
3394
3400
return None
3401
+ if e ._resolved_type is not None :
3402
+ return e ._resolved_type if isinstance (e ._resolved_type , Instance ) else None
3395
3403
keys : List [Type ] = []
3396
3404
values : List [Type ] = []
3397
3405
stargs : Optional [Tuple [Type , Type ]] = None
@@ -3405,17 +3413,22 @@ def fast_dict_type(self, e: DictExpr) -> Optional[Type]:
3405
3413
):
3406
3414
stargs = (st .args [0 ], st .args [1 ])
3407
3415
else :
3416
+ e ._resolved_type = NoneType ()
3408
3417
return None
3409
3418
else :
3410
3419
keys .append (self .accept (key ))
3411
3420
values .append (self .accept (value ))
3412
3421
kt = join .join_type_list (keys )
3413
3422
vt = join .join_type_list (values )
3414
3423
if not (allow_fast_container_literal (kt ) and allow_fast_container_literal (vt )):
3424
+ e ._resolved_type = NoneType ()
3415
3425
return None
3416
3426
if stargs and (stargs [0 ] != kt or stargs [1 ] != vt ):
3427
+ e ._resolved_type = NoneType ()
3417
3428
return None
3418
- return self .chk .named_generic_type ('builtins.dict' , [kt , vt ])
3429
+ dt = self .chk .named_generic_type ('builtins.dict' , [kt , vt ])
3430
+ e ._resolved_type = dt
3431
+ return dt
3419
3432
3420
3433
def visit_dict_expr (self , e : DictExpr ) -> Type :
3421
3434
"""Type check a dict expression.
0 commit comments