From bfc7a88ee6fdc45cf2f948636a484e632d9a4c00 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Mon, 10 Mar 2025 23:12:13 +0000 Subject: [PATCH] Fix crash for callable with *args and suffix against Any --- mypy/constraints.py | 5 ++++- mypy/types.py | 2 +- test-data/unit/check-typevar-tuple.test | 12 ++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mypy/constraints.py b/mypy/constraints.py index d88b722aa1ce..e76f6cd639ad 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -53,6 +53,7 @@ UnionType, UnpackType, find_unpack_in_list, + flatten_nested_tuples, get_proper_type, has_recursive_types, has_type_vars, @@ -1347,7 +1348,9 @@ def visit_type_alias_type(self, template: TypeAliasType) -> list[Constraint]: def infer_against_any(self, types: Iterable[Type], any_type: AnyType) -> list[Constraint]: res: list[Constraint] = [] - for t in types: + # Some items may be things like `*Tuple[*Ts, T]` for example from callable types with + # suffix after *arg, so flatten them. + for t in flatten_nested_tuples(types): if isinstance(t, UnpackType): if isinstance(t.type, TypeVarTupleType): res.append(Constraint(t.type, self.direction, any_type)) diff --git a/mypy/types.py b/mypy/types.py index f9749945d9e9..9dd0ef8552b9 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -3726,7 +3726,7 @@ def find_unpack_in_list(items: Sequence[Type]) -> int | None: return unpack_index -def flatten_nested_tuples(types: Sequence[Type]) -> list[Type]: +def flatten_nested_tuples(types: Iterable[Type]) -> list[Type]: """Recursively flatten TupleTypes nested with Unpack. For example this will transform diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 2cc84c8e6b15..57a96291b04a 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -2606,3 +2606,15 @@ def test(xs: tuple[Unpack[Ts]], xsi: tuple[int, Unpack[Ts]]) -> None: reveal_type(join(xsi, ai)) # N: Revealed type is "Tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]" reveal_type(join(ai, xsi)) # N: Revealed type is "Tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]" [builtins fixtures/tuple.pyi] + +[case testTypeVarTupleInferAgainstAnyCallableSuffix] +from typing import Any, Callable, TypeVar, TypeVarTuple + +Ts = TypeVarTuple("Ts") +R = TypeVar("R") +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]