1515 make_optional_type ,
1616)
1717from 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 ,
2020 PartialType , DeletedType , UninhabitedType , TypeType , TypeOfAny , LiteralType , LiteralValue ,
21- is_named_instance , FunctionLike , ParamSpecDef ,
21+ is_named_instance , FunctionLike , ParamSpecType ,
2222 StarType , is_optional , remove_optional , is_generic_instance , get_proper_type , ProperType ,
2323 get_proper_types , flatten_nested_unions
2424)
@@ -1904,8 +1904,8 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C
19041904 # same thing.
19051905 #
19061906 # 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.
19091909 #
19101910 # (If the user did *not* mean for 'T' to be consistently bound to the
19111911 # 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,
32773277 # Used for list and set expressions, as well as for tuples
32783278 # containing star expressions that don't refer to a
32793279 # 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 ())
32823281 constructor = CallableType (
32833282 [tv ],
32843283 [nodes .ARG_STAR ],
32853284 [None ],
32863285 self .chk .named_generic_type (fullname , [tv ]),
32873286 self .named_type ('builtins.function' ),
32883287 name = tag ,
3289- variables = [tvdef ])
3288+ variables = [tv ])
32903289 out = self .check_call (constructor ,
32913290 [(i .expr if isinstance (i , StarExpr ) else i )
32923291 for i in items ],
@@ -3435,10 +3434,8 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
34353434 tup .column = value .column
34363435 args .append (tup )
34373436 # 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 ())
34423439 rv = None
34433440 # Call dict(*args), unless it's empty and stargs is not.
34443441 if args or not stargs :
@@ -3452,7 +3449,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
34523449 self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
34533450 self .named_type ('builtins.function' ),
34543451 name = '<dict>' ,
3455- variables = [ktdef , vtdef ])
3452+ variables = [kt , vt ])
34563453 rv = self .check_call (constructor , args , [nodes .ARG_POS ] * len (args ), e )[0 ]
34573454 else :
34583455 # dict(...) will be called below.
@@ -3469,7 +3466,7 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
34693466 self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
34703467 self .named_type ('builtins.function' ),
34713468 name = '<list>' ,
3472- variables = [ktdef , vtdef ])
3469+ variables = [kt , vt ])
34733470 rv = self .check_call (constructor , [arg ], [nodes .ARG_POS ], arg )[0 ]
34743471 else :
34753472 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,
37593756
37603757 # Infer the type of the list comprehension by using a synthetic generic
37613758 # 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 ]
37643761 constructor = CallableType (
37653762 tv_list ,
37663763 [nodes .ARG_POS ],
37673764 [None ],
37683765 self .chk .named_generic_type (type_name , tv_list + additional_args ),
37693766 self .chk .named_type ('builtins.function' ),
37703767 name = id_for_messages ,
3771- variables = [tvdef ])
3768+ variables = [tv ])
37723769 return self .check_call (constructor ,
37733770 [gen .left_expr ], [nodes .ARG_POS ], gen )[0 ]
37743771
@@ -3779,15 +3776,13 @@ def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
37793776
37803777 # Infer the type of the list comprehension by using a synthetic generic
37813778 # 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 ())
37863781 constructor = CallableType (
3787- [kt , vt ],
3782+ [ktdef , vtdef ],
37883783 [nodes .ARG_POS , nodes .ARG_POS ],
37893784 [None , None ],
3790- self .chk .named_generic_type ('builtins.dict' , [kt , vt ]),
3785+ self .chk .named_generic_type ('builtins.dict' , [ktdef , vtdef ]),
37913786 self .chk .named_type ('builtins.function' ),
37923787 name = '<dictionary-comprehension>' ,
37933788 variables = [ktdef , vtdef ])
@@ -4450,7 +4445,7 @@ def all_same_types(types: List[Type]) -> bool:
44504445
44514446
44524447def merge_typevars_in_callables_by_name (
4453- callables : Sequence [CallableType ]) -> Tuple [List [CallableType ], List [TypeVarDef ]]:
4448+ callables : Sequence [CallableType ]) -> Tuple [List [CallableType ], List [TypeVarType ]]:
44544449 """Takes all the typevars present in the callables and 'combines' the ones with the same name.
44554450
44564451 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(
44594454 distinct ids.)
44604455
44614456 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.
44644459
44654460 This is useful if we want to take the output lists and "merge" them into one callable
44664461 in some way -- for example, when unioning together overloads.
44674462
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.
44694464 """
4470-
44714465 output : List [CallableType ] = []
44724466 unique_typevars : Dict [str , TypeVarType ] = {}
4473- variables : List [TypeVarDef ] = []
4467+ variables : List [TypeVarType ] = []
44744468
44754469 for target in callables :
44764470 if target .is_generic ():
44774471 target = freshen_function_type_vars (target )
44784472
44794473 rename = {} # Dict[TypeVarId, TypeVar]
4480- for tvdef in target .variables :
4481- name = tvdef .fullname
4474+ for tv in target .variables :
4475+ name = tv .fullname
44824476 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 ):
44854479 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 ]
44904484
44914485 target = cast (CallableType , expand_type (target , rename ))
44924486 output .append (target )
0 commit comments