From c49edce1bb111f6b94bfbbd8b5e88329713e7527 Mon Sep 17 00:00:00 2001 From: Rob Hornby Date: Thu, 9 Nov 2023 02:34:15 +0000 Subject: [PATCH 1/3] Add testTypeVarTupleOverloadArbitraryLength which crashes --- test-data/unit/check-typevar-tuple.test | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index e85863f0ed04..7ca7556b0c5b 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -1789,6 +1789,28 @@ def test(a: Container[Any], b: Container[int], c: Container[str]): reveal_type(build(b, c)) # N: Revealed type is "__main__.Array[builtins.int, builtins.str]" [builtins fixtures/tuple.pyi] +[case testTypeVarTupleOverloadArbitraryLength] +from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload + +T = TypeVar("T") +Ts = TypeVarTuple("Ts") + + +@overload +def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]: + ... +@overload +def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]: + ... +def add(self: Any, other: Any) -> Any: + ... + + +def test(a: Tuple[int, str], b: Tuple[bool], c: Tuple[bool, ...]): + reveal_type(add(a, b)) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]" + reveal_type(add(b, c)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]" +[builtins fixtures/tuple.pyi] + [case testTypeVarTupleIndexOldStyleNonNormalizedAndNonLiteral] from typing import Any, Tuple from typing_extensions import Unpack From eb09ac1567002d22d2be16504c00e2e464a8b319 Mon Sep 17 00:00:00 2001 From: Rob Hornby Date: Thu, 9 Nov 2023 02:35:56 +0000 Subject: [PATCH 2/3] Make testTypeVarTupleOverloadArbitraryLength pass --- mypy/constraints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/constraints.py b/mypy/constraints.py index 49e542a49e56..b47cd80eb064 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -949,7 +949,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]: for item in actual.items: if isinstance(item, UnpackType): unpacked = get_proper_type(item.type) - if isinstance(unpacked, TypeVarType): + if isinstance(unpacked, (TypeVarType, TypeVarTupleType)): # Cannot infer anything for T from [T, ...] <: *Ts continue assert ( From 13421364c27a52ecfbfe52407a8f40c87b17b707 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Fri, 10 Nov 2023 00:24:06 +0000 Subject: [PATCH 3/3] Apply suggestions from code review --- mypy/constraints.py | 2 +- test-data/unit/check-typevar-tuple.test | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/mypy/constraints.py b/mypy/constraints.py index b47cd80eb064..88ede372e011 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -949,7 +949,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]: for item in actual.items: if isinstance(item, UnpackType): unpacked = get_proper_type(item.type) - if isinstance(unpacked, (TypeVarType, TypeVarTupleType)): + if isinstance(unpacked, TypeVarTupleType): # Cannot infer anything for T from [T, ...] <: *Ts continue assert ( diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 7ca7556b0c5b..25babf442d21 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -1794,8 +1794,6 @@ from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload T = TypeVar("T") Ts = TypeVarTuple("Ts") - - @overload def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]: ... @@ -1804,8 +1802,6 @@ def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]: ... def add(self: Any, other: Any) -> Any: ... - - def test(a: Tuple[int, str], b: Tuple[bool], c: Tuple[bool, ...]): reveal_type(add(a, b)) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]" reveal_type(add(b, c)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"