Skip to content

Commit 2d21032

Browse files
author
Ivan Levkivskyi
committed
Couple fixes
1 parent 4f8afce commit 2d21032

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

mypy/checkexpr.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ def check_callable_call(
16061606
lambda i: self.accept(args[i]),
16071607
)
16081608
callee = self.infer_function_type_arguments(
1609-
callee, args, arg_kinds, formal_to_actual, context
1609+
callee, args, arg_kinds, arg_names, formal_to_actual, need_refresh, context
16101610
)
16111611
if need_refresh:
16121612
formal_to_actual = map_actuals_to_formals(
@@ -1896,7 +1896,9 @@ def infer_function_type_arguments(
18961896
callee_type: CallableType,
18971897
args: list[Expression],
18981898
arg_kinds: list[ArgKind],
1899+
arg_names: Sequence[str | None] | None,
18991900
formal_to_actual: list[list[int]],
1901+
need_refresh: bool,
19001902
context: Context,
19011903
) -> CallableType:
19021904
"""Infer the type arguments for a generic callee type.
@@ -1938,7 +1940,14 @@ def infer_function_type_arguments(
19381940
if 2 in arg_pass_nums:
19391941
# Second pass of type inference.
19401942
(callee_type, inferred_args) = self.infer_function_type_arguments_pass2(
1941-
callee_type, args, arg_kinds, formal_to_actual, inferred_args, context
1943+
callee_type,
1944+
args,
1945+
arg_kinds,
1946+
arg_names,
1947+
formal_to_actual,
1948+
inferred_args,
1949+
need_refresh,
1950+
context,
19421951
)
19431952

19441953
if (
@@ -1964,6 +1973,17 @@ def infer_function_type_arguments(
19641973
or set(get_type_vars(a)) & set(callee_type.variables)
19651974
for a in inferred_args
19661975
):
1976+
if need_refresh:
1977+
# Technically we need to refresh formal_to_actual after *each* inference pass,
1978+
# since each pass can expand ParamSpec or TypeVarTuple. Although such situations
1979+
# are very rare, not doing this can cause crashes.
1980+
formal_to_actual = map_actuals_to_formals(
1981+
arg_kinds,
1982+
arg_names,
1983+
callee_type.arg_kinds,
1984+
callee_type.arg_names,
1985+
lambda a: self.accept(args[a]),
1986+
)
19671987
# If the regular two-phase inference didn't work, try inferring type
19681988
# variables while allowing for polymorphic solutions, i.e. for solutions
19691989
# potentially involving free variables.
@@ -2011,8 +2031,10 @@ def infer_function_type_arguments_pass2(
20112031
callee_type: CallableType,
20122032
args: list[Expression],
20132033
arg_kinds: list[ArgKind],
2034+
arg_names: Sequence[str | None] | None,
20142035
formal_to_actual: list[list[int]],
20152036
old_inferred_args: Sequence[Type | None],
2037+
need_refresh: bool,
20162038
context: Context,
20172039
) -> tuple[CallableType, list[Type | None]]:
20182040
"""Perform second pass of generic function type argument inference.
@@ -2034,6 +2056,14 @@ def infer_function_type_arguments_pass2(
20342056
if isinstance(arg, (NoneType, UninhabitedType)) or has_erased_component(arg):
20352057
inferred_args[i] = None
20362058
callee_type = self.apply_generic_arguments(callee_type, inferred_args, context)
2059+
if need_refresh:
2060+
formal_to_actual = map_actuals_to_formals(
2061+
arg_kinds,
2062+
arg_names,
2063+
callee_type.arg_kinds,
2064+
callee_type.arg_names,
2065+
lambda a: self.accept(args[a]),
2066+
)
20372067

20382068
arg_types = self.infer_arg_types_in_context(callee_type, args, arg_kinds, formal_to_actual)
20392069

mypy/constraints.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
918918
if (
919919
type_state.infer_polymorphic
920920
and cactual.variables
921-
and cactual.param_spec() is None
922921
and not self.skip_neg_op
923922
# Technically, the correct inferred type for application of e.g.
924923
# Callable[..., T] -> Callable[..., T] (with literal ellipsis), to a generic

0 commit comments

Comments
 (0)