15
15
make_optional_type ,
16
16
)
17
17
from mypy .types import (
18
- Type , AnyType , CallableType , Overloaded , NoneType , TypeGuardType , TypeVarDef ,
19
- TupleType , TypedDictType , Instance , TypeVarType , ErasedType , UnionType ,
18
+ Type , AnyType , CallableType , Overloaded , NoneType , TypeVarType , TypeGuardType ,
19
+ TupleType , TypedDictType , Instance , ErasedType , UnionType ,
20
20
PartialType , DeletedType , UninhabitedType , TypeType , TypeOfAny , LiteralType , LiteralValue ,
21
- is_named_instance , FunctionLike , ParamSpecDef ,
21
+ is_named_instance , FunctionLike , ParamSpecType ,
22
22
StarType , is_optional , remove_optional , is_generic_instance , get_proper_type , ProperType ,
23
23
get_proper_types , flatten_nested_unions
24
24
)
@@ -1904,8 +1904,8 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C
1904
1904
# same thing.
1905
1905
#
1906
1906
# This function will make sure that all instances of that TypeVar 'T'
1907
- # refer to the same underlying TypeVarType and TypeVarDef objects to
1908
- # simplify the union-ing logic below.
1907
+ # refer to the same underlying TypeVarType objects to simplify the union-ing
1908
+ # logic below.
1909
1909
#
1910
1910
# (If the user did *not* mean for 'T' to be consistently bound to the
1911
1911
# same type in their overloads, well, their code is probably too
@@ -3277,16 +3277,15 @@ def check_lst_expr(self, items: List[Expression], fullname: str,
3277
3277
# Used for list and set expressions, as well as for tuples
3278
3278
# containing star expressions that don't refer to a
3279
3279
# Tuple. (Note: "lst" stands for list-set-tuple. :-)
3280
- tvdef = TypeVarDef ('T' , 'T' , - 1 , [], self .object_type ())
3281
- tv = TypeVarType (tvdef )
3280
+ tv = TypeVarType ('T' , 'T' , - 1 , [], self .object_type ())
3282
3281
constructor = CallableType (
3283
3282
[tv ],
3284
3283
[nodes .ARG_STAR ],
3285
3284
[None ],
3286
3285
self .chk .named_generic_type (fullname , [tv ]),
3287
3286
self .named_type ('builtins.function' ),
3288
3287
name = tag ,
3289
- variables = [tvdef ])
3288
+ variables = [tv ])
3290
3289
out = self .check_call (constructor ,
3291
3290
[(i .expr if isinstance (i , StarExpr ) else i )
3292
3291
for i in items ],
@@ -3435,10 +3434,8 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
3435
3434
tup .column = value .column
3436
3435
args .append (tup )
3437
3436
# Define type variables (used in constructors below).
3438
- ktdef = TypeVarDef ('KT' , 'KT' , - 1 , [], self .object_type ())
3439
- vtdef = TypeVarDef ('VT' , 'VT' , - 2 , [], self .object_type ())
3440
- kt = TypeVarType (ktdef )
3441
- vt = TypeVarType (vtdef )
3437
+ kt = TypeVarType ('KT' , 'KT' , - 1 , [], self .object_type ())
3438
+ vt = TypeVarType ('VT' , 'VT' , - 2 , [], self .object_type ())
3442
3439
rv = None
3443
3440
# Call dict(*args), unless it's empty and stargs is not.
3444
3441
if args or not stargs :
@@ -3452,7 +3449,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
3452
3449
self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
3453
3450
self .named_type ('builtins.function' ),
3454
3451
name = '<dict>' ,
3455
- variables = [ktdef , vtdef ])
3452
+ variables = [kt , vt ])
3456
3453
rv = self .check_call (constructor , args , [nodes .ARG_POS ] * len (args ), e )[0 ]
3457
3454
else :
3458
3455
# dict(...) will be called below.
@@ -3469,7 +3466,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
3469
3466
self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
3470
3467
self .named_type ('builtins.function' ),
3471
3468
name = '<list>' ,
3472
- variables = [ktdef , vtdef ])
3469
+ variables = [kt , vt ])
3473
3470
rv = self .check_call (constructor , [arg ], [nodes .ARG_POS ], arg )[0 ]
3474
3471
else :
3475
3472
self .check_method_call_by_name ('update' , rv , [arg ], [nodes .ARG_POS ], arg )
@@ -3759,16 +3756,16 @@ def check_generator_or_comprehension(self, gen: GeneratorExpr,
3759
3756
3760
3757
# Infer the type of the list comprehension by using a synthetic generic
3761
3758
# callable type.
3762
- tvdef = TypeVarDef ('T' , 'T' , - 1 , [], self .object_type ())
3763
- tv_list : List [Type ] = [TypeVarType ( tvdef ) ]
3759
+ tv = TypeVarType ('T' , 'T' , - 1 , [], self .object_type ())
3760
+ tv_list : List [Type ] = [tv ]
3764
3761
constructor = CallableType (
3765
3762
tv_list ,
3766
3763
[nodes .ARG_POS ],
3767
3764
[None ],
3768
3765
self .chk .named_generic_type (type_name , tv_list + additional_args ),
3769
3766
self .chk .named_type ('builtins.function' ),
3770
3767
name = id_for_messages ,
3771
- variables = [tvdef ])
3768
+ variables = [tv ])
3772
3769
return self .check_call (constructor ,
3773
3770
[gen .left_expr ], [nodes .ARG_POS ], gen )[0 ]
3774
3771
@@ -3779,15 +3776,13 @@ def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
3779
3776
3780
3777
# Infer the type of the list comprehension by using a synthetic generic
3781
3778
# callable type.
3782
- ktdef = TypeVarDef ('KT' , 'KT' , - 1 , [], self .object_type ())
3783
- vtdef = TypeVarDef ('VT' , 'VT' , - 2 , [], self .object_type ())
3784
- kt = TypeVarType (ktdef )
3785
- vt = TypeVarType (vtdef )
3779
+ ktdef = TypeVarType ('KT' , 'KT' , - 1 , [], self .object_type ())
3780
+ vtdef = TypeVarType ('VT' , 'VT' , - 2 , [], self .object_type ())
3786
3781
constructor = CallableType (
3787
- [kt , vt ],
3782
+ [ktdef , vtdef ],
3788
3783
[nodes .ARG_POS , nodes .ARG_POS ],
3789
3784
[None , None ],
3790
- self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
3785
+ self .chk .named_generic_type ('builtins.dict' , [ktdef , vtdef ]),
3791
3786
self .chk .named_type ('builtins.function' ),
3792
3787
name = '<dictionary-comprehension>' ,
3793
3788
variables = [ktdef , vtdef ])
@@ -4450,7 +4445,7 @@ def all_same_types(types: List[Type]) -> bool:
4450
4445
4451
4446
4452
4447
def merge_typevars_in_callables_by_name (
4453
- callables : Sequence [CallableType ]) -> Tuple [List [CallableType ], List [TypeVarDef ]]:
4448
+ callables : Sequence [CallableType ]) -> Tuple [List [CallableType ], List [TypeVarType ]]:
4454
4449
"""Takes all the typevars present in the callables and 'combines' the ones with the same name.
4455
4450
4456
4451
For example, suppose we have two callables with signatures "f(x: T, y: S) -> T" and
@@ -4459,34 +4454,33 @@ def merge_typevars_in_callables_by_name(
4459
4454
distinct ids.)
4460
4455
4461
4456
If we pass in both callables into this function, it returns a a list containing two
4462
- new callables that are identical in signature, but use the same underlying TypeVarDef
4463
- and TypeVarType objects for T and S.
4457
+ new callables that are identical in signature, but use the same underlying TypeVarType
4458
+ for T and S.
4464
4459
4465
4460
This is useful if we want to take the output lists and "merge" them into one callable
4466
4461
in some way -- for example, when unioning together overloads.
4467
4462
4468
- Returns both the new list of callables and a list of all distinct TypeVarDef objects used.
4463
+ Returns both the new list of callables and a list of all distinct TypeVarType objects used.
4469
4464
"""
4470
-
4471
4465
output : List [CallableType ] = []
4472
4466
unique_typevars : Dict [str , TypeVarType ] = {}
4473
- variables : List [TypeVarDef ] = []
4467
+ variables : List [TypeVarType ] = []
4474
4468
4475
4469
for target in callables :
4476
4470
if target .is_generic ():
4477
4471
target = freshen_function_type_vars (target )
4478
4472
4479
4473
rename = {} # Dict[TypeVarId, TypeVar]
4480
- for tvdef in target .variables :
4481
- name = tvdef .fullname
4474
+ for tv in target .variables :
4475
+ name = tv .fullname
4482
4476
if name not in unique_typevars :
4483
- # TODO(shantanu): fix for ParamSpecDef
4484
- if isinstance (tvdef , ParamSpecDef ):
4477
+ # TODO(shantanu): fix for ParamSpecType
4478
+ if isinstance (tv , ParamSpecType ):
4485
4479
continue
4486
- assert isinstance (tvdef , TypeVarDef )
4487
- unique_typevars [name ] = TypeVarType ( tvdef )
4488
- variables .append (tvdef )
4489
- rename [tvdef .id ] = unique_typevars [name ]
4480
+ assert isinstance (tv , TypeVarType )
4481
+ unique_typevars [name ] = tv
4482
+ variables .append (tv )
4483
+ rename [tv .id ] = unique_typevars [name ]
4490
4484
4491
4485
target = cast (CallableType , expand_type (target , rename ))
4492
4486
output .append (target )
0 commit comments