[ty] Fix Todo type for starred elements in tuple expressions

This commit is contained in:
Alex Waygood 2025-11-17 12:02:51 +00:00
parent 665f68036c
commit 1fee6c3ede
8 changed files with 184 additions and 39 deletions

View file

@ -42,6 +42,12 @@ def f[T](x: T, cond: bool) -> T | list[T]:
return x if cond else [x]
l5: int | list[int] = f(1, True)
a: list[int] = [1, 2, *(3, 4, 5)]
reveal_type(a) # revealed: list[int]
b: list[list[int]] = [[1], [2], *([3], [4])]
reveal_type(b) # revealed: list[list[int]]
```
`typed_dict.py`:

View file

@ -43,13 +43,13 @@ reveal_type(len((1,))) # revealed: Literal[1]
reveal_type(len((1, 2))) # revealed: Literal[2]
reveal_type(len(tuple())) # revealed: Literal[0]
# TODO: Handle star unpacks; Should be: Literal[0]
reveal_type(len((*[],))) # revealed: Literal[1]
# could also be `Literal[0]`, but `int` is accurate
reveal_type(len((*[],))) # revealed: int
# fmt: off
# TODO: Handle star unpacks; Should be: Literal[1]
reveal_type(len( # revealed: Literal[2]
# could also be `Literal[1]`, but `int` is accurate
reveal_type(len( # revealed: int
(
*[],
1,
@ -58,11 +58,11 @@ reveal_type(len( # revealed: Literal[2]
# fmt: on
# TODO: Handle star unpacks; Should be: Literal[2]
reveal_type(len((*[], 1, 2))) # revealed: Literal[3]
# Could also be `Literal[2]`, but `int` is accurate
reveal_type(len((*[], 1, 2))) # revealed: int
# TODO: Handle star unpacks; Should be: Literal[0]
reveal_type(len((*[], *{}))) # revealed: Literal[2]
# Could also be `Literal[0]`, but `int` is accurate
reveal_type(len((*[], *{}))) # revealed: int
```
Tuple subclasses:

View file

@ -531,4 +531,18 @@ x: list[Literal[1, 2, 3]] = list((1, 2, 3))
reveal_type(x) # revealed: list[Literal[1, 2, 3]]
```
## Tuples with starred elements
```py
x = (1, *range(3), 3)
reveal_type(x) # revealed: tuple[Literal[1], *tuple[int, ...], Literal[3]]
y = 1, 2
reveal_type(("foo", *y)) # revealed: tuple[Literal["foo"], Literal[1], Literal[2]]
aa: tuple[list[int], ...] = ([42], *{[56], [78]}, [100])
reveal_type(aa) # revealed: tuple[list[int], *tuple[list[int], ...], list[int]]
```
[not a singleton type]: https://discuss.python.org/t/should-we-specify-in-the-language-reference-that-the-empty-tuple-is-a-singleton/67957