@@ -2099,6 +2099,17 @@ def infer_function_type_arguments(
20992099 pass1_args .append (arg )
21002100
21012101 if True : # NEW CODE
2102+ # compute the inner constraints
2103+ inner_constraints = infer_constraints_for_callable (
2104+ callee_type ,
2105+ pass1_args ,
2106+ arg_kinds ,
2107+ arg_names ,
2108+ formal_to_actual ,
2109+ context = self .argument_infer_context (),
2110+ )
2111+
2112+ # compute the outer solution
21022113 outer_constraints = self .infer_constraints_from_context (callee_type , context )
21032114 outer_solution = solve_constraints (
21042115 callee_type .variables ,
@@ -2116,34 +2127,7 @@ def infer_function_type_arguments(
21162127 )
21172128 outer_ret_type = get_proper_type (outer_callee .ret_type )
21182129
2119- # NOTE: inner solution not needed
2120- inner_constraints = infer_constraints_for_callable (
2121- callee_type ,
2122- pass1_args ,
2123- arg_kinds ,
2124- arg_names ,
2125- formal_to_actual ,
2126- context = self .argument_infer_context (),
2127- )
2128- inner_solution = solve_constraints (
2129- callee_type .variables ,
2130- inner_constraints ,
2131- strict = self .chk .in_checked_function (),
2132- allow_polymorphic = False ,
2133- )
2134- inner_args = [
2135- None if has_uninhabited_component (arg ) or has_erased_component (arg ) else arg
2136- for arg in inner_solution [0 ]
2137- ]
2138- inner_solution = (inner_args , inner_solution [1 ])
2139- # inner_callee = self.apply_generic_arguments(
2140- # callee_type,
2141- # inner_solution[0], # no filtering here
2142- # context,
2143- # skip_unsatisfied=True,
2144- # )
2145- # inner_ret_type = get_proper_type(inner_callee.ret_type)
2146-
2130+ # compute the joint solution using both inner and outer constraints.
21472131 # NOTE: The order of constraints is important here!
21482132 # solve(outer + inner) and solve(inner + outer) may yield different results.
21492133 # we need to use outer first.
@@ -2163,27 +2147,7 @@ def infer_function_type_arguments(
21632147 )
21642148 joint_ret_type = get_proper_type (joint_callee .ret_type )
21652149
2166- # Now, we select which solution to use.
2167- use_joint = True
2168- use_outer = False
2169- use_inner = False
2170-
2171- # NOTE: inner solution not needed
2172- # if (
2173- # # joint constraints failed to produce a complete solution
2174- # None in joint_solution[0]
2175- # # If the inner solution is more concrete than the joint solution, prefer the inner solution.
2176- # or is_subtype(inner_ret_type, joint_ret_type)
2177- # or ( # HACK to fix testLiteralAndGenericWithUnion
2178- # isinstance(inner_ret_type, UnionType)
2179- # and any(is_subtype(val, joint_ret_type) for val in inner_ret_type.items)
2180- # )
2181- # ):
2182- # use_joint = False
2183- # use_outer = False
2184- # use_inner = True
2185-
2186- if (
2150+ if ( # determine which solution to take
21872151 # joint constraints failed to produce a complete solution
21882152 None in joint_solution [0 ]
21892153 # If the outer solution is more concrete than the joint solution, prefer the outer solution.
@@ -2194,14 +2158,12 @@ def infer_function_type_arguments(
21942158 )
21952159 ):
21962160 use_joint = False
2197- use_outer = True
2198- use_inner = False
2161+ else :
2162+ use_joint = True
21992163
22002164 if use_joint :
22012165 inferred_args = joint_solution [0 ]
2202- elif use_inner :
2203- inferred_args = inner_solution [0 ]
2204- elif use_outer :
2166+ else :
22052167 # If we cannot use the joint solution, fallback to outer_solution
22062168 inferred_args = outer_solution [0 ]
22072169 # Don't show errors after we have only used the outer context for inference.
@@ -2238,8 +2200,6 @@ def infer_function_type_arguments(
22382200 allow_polymorphic = False ,
22392201 )
22402202 inferred_args = inner_solution [0 ]
2241- else :
2242- raise RuntimeError ("No solution found for function type arguments" )
22432203 else : # END NEW CODE
22442204 pass
22452205
0 commit comments