Skip to content

Commit 9e2198f

Browse files
authored
Fix crash on type inference against non-normal callables (#18858)
Fixes #17755 Fix is trivial, so not really waiting for review. Btw I found few other places where we do not normalize callables. TBH I already forgot when we actually _need_ to normalize, but I don't want to just blanket add normalization, as it may be a relatively expensive function. If we will hit another similar crash, I will add more normalization accordingly (similar to what I did with kwargs unpacking).
1 parent 6badb4a commit 9e2198f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

mypy/constraints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,11 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
10631063
# using e.g. callback protocols.
10641064
# TODO: check that callables match? Ideally we should not infer constraints
10651065
# callables that can never be subtypes of one another in given direction.
1066-
template = template.with_unpacked_kwargs()
1066+
template = template.with_unpacked_kwargs().with_normalized_var_args()
10671067
extra_tvars = False
10681068
if isinstance(self.actual, CallableType):
10691069
res: list[Constraint] = []
1070-
cactual = self.actual.with_unpacked_kwargs()
1070+
cactual = self.actual.with_unpacked_kwargs().with_normalized_var_args()
10711071
param_spec = template.param_spec()
10721072

10731073
template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ def with_normalized_var_args(self) -> Self:
22102210
new_unpack = nested_unpacked.args[0]
22112211
else:
22122212
if not isinstance(nested_unpacked, TypeVarTupleType):
2213-
# We found a non-nomralized tuple type, this means this method
2213+
# We found a non-normalized tuple type, this means this method
22142214
# is called during semantic analysis (e.g. from get_proper_type())
22152215
# there is no point in normalizing callables at this stage.
22162216
return self

test-data/unit/check-typevar-tuple.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,3 +2618,13 @@ def deco(func: Callable[[*Ts, int], R]) -> Callable[[*Ts], R]:
26182618
untyped: Any
26192619
reveal_type(deco(untyped)) # N: Revealed type is "def (*Any) -> Any"
26202620
[builtins fixtures/tuple.pyi]
2621+
2622+
[case testNoCrashOnNonNormalUnpackInCallable]
2623+
from typing import Callable, Unpack, TypeVar
2624+
2625+
T = TypeVar("T")
2626+
def fn(f: Callable[[*tuple[T]], int]) -> Callable[[*tuple[T]], int]: ...
2627+
2628+
def test(*args: Unpack[tuple[T]]) -> int: ...
2629+
reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int"
2630+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)