From 59f8e25224ba84b6fc5611e204489d7779b9451b Mon Sep 17 00:00:00 2001 From: STerliakov Date: Sat, 17 May 2025 03:52:42 +0200 Subject: [PATCH 1/2] Include fallback in building constraints for tuple types --- mypy/constraints.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mypy/constraints.py b/mypy/constraints.py index 8e7a30e05ffb..ec7b3041affc 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -1335,6 +1335,11 @@ def visit_tuple_type(self, template: TupleType) -> list[Constraint]: res.extend( infer_constraints(template_items[i], actual_items[i], self.direction) ) + res.extend( + infer_constraints( + template.partial_fallback, actual.partial_fallback, self.direction + ) + ) return res elif isinstance(actual, AnyType): return self.infer_against_any(template.items, actual) From 8b23cf0f75216135f1b3ad271a7f28cef5b5b6a6 Mon Sep 17 00:00:00 2001 From: STerliakov Date: Sat, 17 May 2025 04:08:38 +0200 Subject: [PATCH 2/2] Add a testcase --- test-data/unit/check-typevar-tuple.test | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index d364439f22e9..3505338977f3 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -2628,3 +2628,19 @@ 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] + +[case testConstraintsIncludeTupleFallback] +from typing import Generic, TypeVar +from typing_extensions import TypeVarTuple, Unpack + +T = TypeVar("T") +Ts = TypeVarTuple("Ts") +_FT = TypeVar("_FT", bound=type) + +def identity(smth: _FT) -> _FT: + return smth + +@identity +class S(tuple[Unpack[Ts]], Generic[T, Unpack[Ts]]): + def f(self, x: T, /) -> T: ... +[builtins fixtures/tuple.pyi]