@@ -1671,6 +1671,10 @@ def check_call(
16711671 object_type ,
16721672 original_type = callee ,
16731673 )
1674+ elif isinstance (callee , UninhabitedType ):
1675+ ret = UninhabitedType ()
1676+ ret .ambiguous = callee .ambiguous
1677+ return callee , ret
16741678 else :
16751679 return self .msg .not_callable (callee , context ), AnyType (TypeOfAny .from_error )
16761680
@@ -1751,14 +1755,6 @@ def check_callable_call(
17511755 return AnyType (TypeOfAny .from_error ), callee
17521756 seen_unpack = True
17531757
1754- formal_to_actual = map_actuals_to_formals (
1755- arg_kinds ,
1756- arg_names ,
1757- callee .arg_kinds ,
1758- callee .arg_names ,
1759- lambda i : self .accept (args [i ]),
1760- )
1761-
17621758 # This is tricky: return type may contain its own type variables, like in
17631759 # def [S] (S) -> def [T] (T) -> tuple[S, T], so we need to update their ids
17641760 # to avoid possible id clashes if this call itself appears in a generic
@@ -1769,27 +1765,29 @@ def check_callable_call(
17691765 freeze_all_type_vars (fresh_ret_type )
17701766 callee = callee .copy_modified (ret_type = fresh_ret_type )
17711767
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+
17721780 if callee .is_generic ():
17731781 need_refresh = any (
17741782 isinstance (v , (ParamSpecType , TypeVarTupleType )) for v in callee .variables
17751783 )
1776- callee = freshen_function_type_vars (callee )
1777- callee = self .infer_function_type_arguments_using_context (callee , context )
1778- if need_refresh :
1779- # Argument kinds etc. may have changed due to
1780- # ParamSpec or TypeVarTuple variables being replaced with an arbitrary
1781- # number of arguments; recalculate actual-to-formal map
1782- formal_to_actual = map_actuals_to_formals (
1783- arg_kinds ,
1784- arg_names ,
1785- callee .arg_kinds ,
1786- callee .arg_names ,
1787- lambda i : self .accept (args [i ]),
1788- )
17891784 callee = self .infer_function_type_arguments (
17901785 callee , args , arg_kinds , arg_names , formal_to_actual , need_refresh , context
17911786 )
17921787 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
17931791 formal_to_actual = map_actuals_to_formals (
17941792 arg_kinds ,
17951793 arg_names ,
@@ -2254,6 +2252,11 @@ def infer_function_type_arguments_pass2(
22542252 if isinstance (arg , (NoneType , UninhabitedType )) or has_erased_component (arg ):
22552253 inferred_args [i ] = None
22562254 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+
22572260 if need_refresh :
22582261 formal_to_actual = map_actuals_to_formals (
22592262 arg_kinds ,
@@ -3465,7 +3468,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
34653468 # It's actually a type expression X | Y.
34663469 return self .accept (e .analyzed )
34673470 if e .op == "and" or e .op == "or" :
3468- return self .check_boolean_op (e , e )
3471+ return self .check_boolean_op (e )
34693472 if e .op == "*" and isinstance (e .left , ListExpr ):
34703473 # Expressions of form [...] * e get special type inference.
34713474 return self .check_list_multiply (e )
@@ -4252,20 +4255,18 @@ def check_op(
42524255 context = context ,
42534256 )
42544257
4255- def check_boolean_op (self , e : OpExpr , context : Context ) -> Type :
4258+ def check_boolean_op (self , e : OpExpr ) -> Type :
42564259 """Type check a boolean operation ('and' or 'or')."""
42574260
42584261 # A boolean operation can evaluate to either of the operands.
42594262
4260- # 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
42614264 # the left operand. We also use the left operand type to guide the type
42624265 # inference of the right operand so that expressions such as
42634266 # '[1] or []' are inferred correctly.
42644267 ctx = self .type_context [- 1 ]
42654268 left_type = self .accept (e .left , ctx )
4266- expanded_left_type = try_expanding_sum_type_to_union (
4267- self .accept (e .left , ctx ), "builtins.bool"
4268- )
4269+ expanded_left_type = try_expanding_sum_type_to_union (left_type , "builtins.bool" )
42694270
42704271 assert e .op in ("and" , "or" ) # Checked by visit_op_expr
42714272
0 commit comments