Skip to content

Commit 925a3ea

Browse files
Simplified away early inner_solution computation
1 parent 26e8da2 commit 925a3ea

File tree

3 files changed

+19
-59
lines changed

3 files changed

+19
-59
lines changed

mypy/checkexpr.py

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test-data/unit/check-functions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3384,7 +3384,7 @@ def f(x: T, y: S) -> Union[T, S]: ...
33843384
def g(x: T, y: S) -> Union[T, S]: ...
33853385

33863386
x = [f, g]
3387-
reveal_type(x) # N: Revealed type is "builtins.list[def [T, S] (x: T`6, y: S`7) -> Union[T`6, S`7]]"
3387+
reveal_type(x) # N: Revealed type is "builtins.list[def [T, S] (x: T`4, y: S`5) -> Union[T`4, S`5]]"
33883388
[builtins fixtures/list.pyi]
33893389

33903390
[case testTypeVariableClashErrorMessage]

test-data/unit/check-generics.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,8 +2929,8 @@ def mix(fs: List[Callable[[S], T]]) -> Callable[[S], List[T]]:
29292929
def id(__x: U) -> U:
29302930
...
29312931
fs = [id, id, id]
2932-
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`15) -> builtins.list[S`15]"
2933-
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`17) -> builtins.list[S`17]"
2932+
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`11) -> builtins.list[S`11]"
2933+
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`13) -> builtins.list[S`13]"
29342934
[builtins fixtures/list.pyi]
29352935

29362936
[case testInferenceAgainstGenericCurry]

0 commit comments

Comments
 (0)