From 2787ffd1bcdb4b667dfc197af6d8ca9e999bb964 Mon Sep 17 00:00:00 2001 From: Omkar Kabde Date: Sat, 18 Jul 2026 06:42:30 +0530 Subject: [PATCH] Fix spurious has-type error when unpacking a TypeVarTuple with a constrained TypeVar --- mypy/treetransform.py | 4 +++- test-data/unit/check-typevar-tuple.test | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mypy/treetransform.py b/mypy/treetransform.py index 25092de66a149..8f6ccbc3e0ec9 100644 --- a/mypy/treetransform.py +++ b/mypy/treetransform.py @@ -450,7 +450,9 @@ def visit_match_stmt(self, o: MatchStmt) -> MatchStmt: ) def visit_star_expr(self, node: StarExpr) -> StarExpr: - return StarExpr(node.expr) + new = StarExpr(self.expr(node.expr)) + new.valid = node.valid + return new def visit_int_expr(self, node: IntExpr) -> IntExpr: return IntExpr(node.value) diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 7db0fa6f4388c..8ce41a905311f 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -2921,3 +2921,22 @@ def g(x: K) -> None: assert d is not None reveal_type(d) # N: Revealed type is "tuple[K | list[int], ...] | tuple[*tuple[int | list[int], ...], int]" [builtins fixtures/tuple.pyi] + +[case testTypeVarTupleUnpackVarWithConstrainedTypeVarExpansion] +# Regression test for https://github.com/python/mypy/issues/21693 +from typing import Generic, Tuple, TypeVar +from typing_extensions import Literal, TypeVarTuple, Unpack + +T = TypeVar("T") +P = TypeVar("P", Literal[True], Literal[False]) +Ts = TypeVarTuple("Ts") + +class Container(Generic[T, P]): + def foo(self) -> T: ... + +def f(container: Container[Tuple[Unpack[Ts]], P]) -> None: + result = container.foo() + reveal_type(result) # N: Revealed type is "tuple[Unpack[Ts`-1]]" + y = (*result,) + reveal_type(y) # N: Revealed type is "tuple[Unpack[Ts`-1]]" +[builtins fixtures/tuple.pyi]