@@ -1755,14 +1755,6 @@ def check_callable_call(
1755
1755
return AnyType (TypeOfAny .from_error ), callee
1756
1756
seen_unpack = True
1757
1757
1758
- formal_to_actual = map_actuals_to_formals (
1759
- arg_kinds ,
1760
- arg_names ,
1761
- callee .arg_kinds ,
1762
- callee .arg_names ,
1763
- lambda i : self .accept (args [i ]),
1764
- )
1765
-
1766
1758
# This is tricky: return type may contain its own type variables, like in
1767
1759
# def [S] (S) -> def [T] (T) -> tuple[S, T], so we need to update their ids
1768
1760
# to avoid possible id clashes if this call itself appears in a generic
@@ -1773,27 +1765,29 @@ def check_callable_call(
1773
1765
freeze_all_type_vars (fresh_ret_type )
1774
1766
callee = callee .copy_modified (ret_type = fresh_ret_type )
1775
1767
1768
+ if callee .is_generic ():
1769
+ callee = freshen_function_type_vars (callee )
1770
+ callee = self .infer_function_type_arguments_using_context (callee , context )
1771
+
1772
+ formal_to_actual = map_actuals_to_formals (
1773
+ arg_kinds ,
1774
+ arg_names ,
1775
+ callee .arg_kinds ,
1776
+ callee .arg_names ,
1777
+ lambda i : self .accept (args [i ]),
1778
+ )
1779
+
1776
1780
if callee .is_generic ():
1777
1781
need_refresh = any (
1778
1782
isinstance (v , (ParamSpecType , TypeVarTupleType )) for v in callee .variables
1779
1783
)
1780
- callee = freshen_function_type_vars (callee )
1781
- callee = self .infer_function_type_arguments_using_context (callee , context )
1782
- if need_refresh :
1783
- # Argument kinds etc. may have changed due to
1784
- # ParamSpec or TypeVarTuple variables being replaced with an arbitrary
1785
- # number of arguments; recalculate actual-to-formal map
1786
- formal_to_actual = map_actuals_to_formals (
1787
- arg_kinds ,
1788
- arg_names ,
1789
- callee .arg_kinds ,
1790
- callee .arg_names ,
1791
- lambda i : self .accept (args [i ]),
1792
- )
1793
1784
callee = self .infer_function_type_arguments (
1794
1785
callee , args , arg_kinds , arg_names , formal_to_actual , need_refresh , context
1795
1786
)
1796
1787
if need_refresh :
1788
+ # Argument kinds etc. may have changed due to
1789
+ # ParamSpec or TypeVarTuple variables being replaced with an arbitrary
1790
+ # number of arguments; recalculate actual-to-formal map
1797
1791
formal_to_actual = map_actuals_to_formals (
1798
1792
arg_kinds ,
1799
1793
arg_names ,
@@ -2258,6 +2252,11 @@ def infer_function_type_arguments_pass2(
2258
2252
if isinstance (arg , (NoneType , UninhabitedType )) or has_erased_component (arg ):
2259
2253
inferred_args [i ] = None
2260
2254
callee_type = self .apply_generic_arguments (callee_type , inferred_args , context )
2255
+
2256
+ if not callee_type .is_generic ():
2257
+ # Fast path, second pass can't give new information.
2258
+ return callee_type , []
2259
+
2261
2260
if need_refresh :
2262
2261
formal_to_actual = map_actuals_to_formals (
2263
2262
arg_kinds ,
@@ -3469,7 +3468,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
3469
3468
# It's actually a type expression X | Y.
3470
3469
return self .accept (e .analyzed )
3471
3470
if e .op == "and" or e .op == "or" :
3472
- return self .check_boolean_op (e , e )
3471
+ return self .check_boolean_op (e )
3473
3472
if e .op == "*" and isinstance (e .left , ListExpr ):
3474
3473
# Expressions of form [...] * e get special type inference.
3475
3474
return self .check_list_multiply (e )
@@ -4256,20 +4255,18 @@ def check_op(
4256
4255
context = context ,
4257
4256
)
4258
4257
4259
- def check_boolean_op (self , e : OpExpr , context : Context ) -> Type :
4258
+ def check_boolean_op (self , e : OpExpr ) -> Type :
4260
4259
"""Type check a boolean operation ('and' or 'or')."""
4261
4260
4262
4261
# A boolean operation can evaluate to either of the operands.
4263
4262
4264
- # We use the current type context to guide the type inference of of
4263
+ # We use the current type context to guide the type inference of
4265
4264
# the left operand. We also use the left operand type to guide the type
4266
4265
# inference of the right operand so that expressions such as
4267
4266
# '[1] or []' are inferred correctly.
4268
4267
ctx = self .type_context [- 1 ]
4269
4268
left_type = self .accept (e .left , ctx )
4270
- expanded_left_type = try_expanding_sum_type_to_union (
4271
- self .accept (e .left , ctx ), "builtins.bool"
4272
- )
4269
+ expanded_left_type = try_expanding_sum_type_to_union (left_type , "builtins.bool" )
4273
4270
4274
4271
assert e .op in ("and" , "or" ) # Checked by visit_op_expr
4275
4272
0 commit comments