Skip to content

Commit 49652b4

Browse files
breaking change: A plain tuple type is no longer represented by a TupleTypeHint, but instead by a ClassTypeHint.
1 parent 5152d55 commit 49652b4

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

.changelog/_unreleased.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[entries]]
2+
id = "a644d3b9-b9f7-4c9c-b240-2be2599e70d5"
3+
type = "breaking change"
4+
description = "A plain `tuple` type is no longer represented by a `TupleTypeHint`, but instead by a `ClassTypeHint`."
5+
author = "@NiklasRosenstein"

src/typeapi/typehint.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _make_wrapper(cls, hint: object, source: "Any | None") -> "TypeHint":
8989
return AnnotatedTypeHint(hint, source)
9090
elif isinstance(hint, TypeVar):
9191
return TypeVarTypeHint(hint, source)
92-
elif origin == tuple or hint == tuple:
92+
elif origin == tuple:
9393
return TupleTypeHint(hint, source)
9494

9595
return ClassTypeHint(hint, source)
@@ -473,13 +473,17 @@ def expr(self) -> str:
473473

474474

475475
class TupleTypeHint(ClassTypeHint):
476+
"""
477+
A special class to represent a type hint for a parameterized tuple. This class does not represent a plain tuple
478+
type without parameterization.
479+
"""
480+
476481
def __init__(self, hint: object, source: "Any | None") -> None:
477482
super().__init__(hint, source)
478483
if self._args == ((),):
479484
self._args = ()
480485
elif self._args == () and self._hint == tuple:
481-
self._args = (Any, ...)
482-
self._origin = tuple
486+
raise ValueError(f"TupleTypeHint can only represent a parameterized tuple.")
483487
if ... in self._args:
484488
assert self._args[-1] == ..., "Tuple Ellipsis not as last arg"
485489
assert len(self._args) == 2, "Tuple with Ellipsis has more than two args"

src/typeapi/typehint_test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ class B:
381381
assert hint.evaluate() == TypeHint(str)
382382

383383

384+
def test__TypeHint__tuple_evaluate() -> None:
385+
assert TypeHint(tuple, source=tuple).evaluate() == TypeHint(tuple)
386+
387+
384388
def test__TypeHint__caching_same_named_type_hints() -> None:
385389
"""
386390
This test ensures that type hint caching is stable if two different
@@ -435,13 +439,12 @@ def tupth(x: Any) -> TupleTypeHint:
435439

436440
def test__TypeHint__native_tuple_type() -> None:
437441
hint = TypeHint(tuple)
438-
assert isinstance(hint, TupleTypeHint), hint
439-
assert len(hint) == 1
442+
assert isinstance(hint, ClassTypeHint), hint
443+
assert len(hint) == 0
440444
assert hint.hint == tuple
441-
assert hint.origin == tuple
442-
assert hint.args == (Any,)
445+
assert hint.origin is None
446+
assert hint.args == ()
443447
assert hint.parameters == ()
444-
assert hint.repeated
445448
assert hint.bases == (object,)
446449

447450
hint = TypeHint(Tuple[Any, ...])

0 commit comments

Comments
 (0)