Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on type inference against non-normal callables #18858

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,11 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
# using e.g. callback protocols.
# TODO: check that callables match? Ideally we should not infer constraints
# callables that can never be subtypes of one another in given direction.
template = template.with_unpacked_kwargs()
template = template.with_unpacked_kwargs().with_normalized_var_args()
extra_tvars = False
if isinstance(self.actual, CallableType):
res: list[Constraint] = []
cactual = self.actual.with_unpacked_kwargs()
cactual = self.actual.with_unpacked_kwargs().with_normalized_var_args()
param_spec = template.param_spec()

template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
Expand Down
2 changes: 1 addition & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,7 @@ def with_normalized_var_args(self) -> Self:
new_unpack = nested_unpacked.args[0]
else:
if not isinstance(nested_unpacked, TypeVarTupleType):
# We found a non-nomralized tuple type, this means this method
# We found a non-normalized tuple type, this means this method
# is called during semantic analysis (e.g. from get_proper_type())
# there is no point in normalizing callables at this stage.
return self
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2618,3 +2618,13 @@ def deco(func: Callable[[*Ts, int], R]) -> Callable[[*Ts], R]:
untyped: Any
reveal_type(deco(untyped)) # N: Revealed type is "def (*Any) -> Any"
[builtins fixtures/tuple.pyi]

[case testNoCrashOnNonNormalUnpackInCallable]
from typing import Callable, Unpack, TypeVar

T = TypeVar("T")
def fn(f: Callable[[*tuple[T]], int]) -> Callable[[*tuple[T]], int]: ...

def test(*args: Unpack[tuple[T]]) -> int: ...
reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int"
[builtins fixtures/tuple.pyi]