@@ -470,8 +470,7 @@ def infer_function_type_arguments_using_context(
470
470
new_args .append (None )
471
471
else :
472
472
new_args .append (arg )
473
- return cast (CallableType , self .apply_generic_arguments (callable , new_args ,
474
- error_context ))
473
+ return self .apply_generic_arguments (callable , new_args , error_context )
475
474
476
475
def infer_function_type_arguments (self , callee_type : CallableType ,
477
476
args : List [Expression ],
@@ -561,9 +560,8 @@ def infer_function_type_arguments_pass2(
561
560
for i , arg in enumerate (inferred_args ):
562
561
if isinstance (arg , (NoneTyp , UninhabitedType )) or has_erased_component (arg ):
563
562
inferred_args [i ] = None
563
+ callee_type = self .apply_generic_arguments (callee_type , inferred_args , context )
564
564
565
- callee_type = cast (CallableType , self .apply_generic_arguments (
566
- callee_type , inferred_args , context ))
567
565
arg_types = self .infer_arg_types_in_context2 (
568
566
callee_type , args , arg_kinds , formal_to_actual )
569
567
@@ -609,8 +607,7 @@ def apply_inferred_arguments(self, callee_type: CallableType,
609
607
# Apply the inferred types to the function type. In this case the
610
608
# return type must be CallableType, since we give the right number of type
611
609
# arguments.
612
- return cast (CallableType , self .apply_generic_arguments (callee_type ,
613
- inferred_args , context ))
610
+ return self .apply_generic_arguments (callee_type , inferred_args , context )
614
611
615
612
def check_argument_count (self , callee : CallableType , actual_types : List [Type ],
616
613
actual_kinds : List [int ], actual_names : List [str ],
@@ -724,10 +721,10 @@ def check_argument_types(self, arg_types: List[Type], arg_kinds: List[int],
724
721
725
722
# There may be some remaining tuple varargs items that haven't
726
723
# been checked yet. Handle them.
724
+ tuplet = arg_types [actual ]
727
725
if (callee .arg_kinds [i ] == nodes .ARG_STAR and
728
726
arg_kinds [actual ] == nodes .ARG_STAR and
729
- isinstance (arg_types [actual ], TupleType )):
730
- tuplet = cast (TupleType , arg_types [actual ])
727
+ isinstance (tuplet , TupleType )):
731
728
while tuple_counter [0 ] < len (tuplet .items ):
732
729
actual_type = get_actual_type (arg_type ,
733
730
arg_kinds [actual ],
@@ -880,22 +877,10 @@ def check_arg(caller_type: Type, original_caller_type: Type, caller_kind: int,
880
877
return ok
881
878
882
879
def apply_generic_arguments (self , callable : CallableType , types : List [Type ],
883
- context : Context ) -> Type :
880
+ context : Context ) -> CallableType :
884
881
"""Simple wrapper around mypy.applytype.apply_generic_arguments."""
885
882
return applytype .apply_generic_arguments (callable , types , self .msg , context )
886
883
887
- def apply_generic_arguments2 (self , overload : Overloaded , types : List [Type ],
888
- context : Context ) -> Type :
889
- items = [] # type: List[CallableType]
890
- for item in overload .items ():
891
- applied = self .apply_generic_arguments (item , types , context )
892
- if isinstance (applied , CallableType ):
893
- items .append (applied )
894
- else :
895
- # There was an error.
896
- return AnyType ()
897
- return Overloaded (items )
898
-
899
884
def visit_member_expr (self , e : MemberExpr ) -> Type :
900
885
"""Visit member expression (of form e.id)."""
901
886
self .chk .module_refs .update (extract_refexpr_names (e ))
@@ -1375,9 +1360,19 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
1375
1360
"""Type check a type application (expr[type, ...])."""
1376
1361
tp = self .accept (tapp .expr )
1377
1362
if isinstance (tp , CallableType ):
1363
+ if len (tp .variables ) != len (tapp .types ):
1364
+ self .msg .incompatible_type_application (len (tp .variables ),
1365
+ len (tapp .types ), tapp )
1366
+ return AnyType ()
1378
1367
return self .apply_generic_arguments (tp , tapp .types , tapp )
1379
- if isinstance (tp , Overloaded ):
1380
- return self .apply_generic_arguments2 (tp , tapp .types , tapp )
1368
+ elif isinstance (tp , Overloaded ):
1369
+ for item in tp .items ():
1370
+ if len (item .variables ) != len (tapp .types ):
1371
+ self .msg .incompatible_type_application (len (item .variables ),
1372
+ len (tapp .types ), tapp )
1373
+ return AnyType ()
1374
+ return Overloaded ([self .apply_generic_arguments (item , tapp .types , tapp )
1375
+ for item in tp .items ()])
1381
1376
return AnyType ()
1382
1377
1383
1378
def visit_type_alias_expr (self , alias : TypeAliasExpr ) -> Type :
@@ -1569,9 +1564,8 @@ def infer_lambda_type_using_context(self, e: FuncExpr) -> CallableType:
1569
1564
# they must be considered as indeterminate. We use ErasedType since it
1570
1565
# does not affect type inference results (it is for purposes like this
1571
1566
# only).
1572
- ctx = replace_meta_vars (ctx , ErasedType ())
1573
-
1574
- callable_ctx = cast (CallableType , ctx )
1567
+ callable_ctx = replace_meta_vars (ctx , ErasedType ())
1568
+ assert isinstance (callable_ctx , CallableType )
1575
1569
1576
1570
arg_kinds = [arg .kind for arg in e .arguments ]
1577
1571
0 commit comments