microsoft / microsoft/pyright

Unpacking a tuple containing an unpacked TypeVarTuple results in an illegal list type inference

Open
#10,637 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

In the following code:

```
from typing import reveal_type

def f[*Args, T](args: tuple[*Args], extra: T) -> None:
*unpacked, _remaining = (*args, extra)
reveal_type(unpacked) # list[*Args@f]
_illegal: list[*Args] # TypeVarTuple is not allowed in this context
reveal_type(tuple(unpacked)) # tuple[Union[*Args@f]]
```

unpacking the combined tuple should result in `unpacked` being inferred as `list[Union[*Args@f]]`. But instead, it is inferred as `list[*Args@f]`, which isn't an actually meaningful type since you cannot unpack a variadic type there. This also is rejected by pyright when using it directly as an annotation. Curiously, when converting the unpacked list into a tuple, its element type is correctly inferred to being the union of types in the TypeVarTuple.

I'm using the VSCode extension with pyright version 1.1.401. The same behaviour also occurrs on the current [pyright playground](https://pyright-play.net/?pyrightVersion=1.1.400&pythonVersion=3.14&code=GYJw9gtgBALgngBwJYDsDmUkQWEMogCmAboQIYA2A%2BvAoQFD0AmhwUwA2gFQCCIaAZwA0UACoBdABRl%2BAgFywArggqFufQeJGEAHjBBkFogJRQAtAD4oAOTApCc%2BlGdQuilAjIBjANaEmIlREEGSoqBgAvFCSXDKC2noGxk4uRKSUNIiEku6evv7JLlBUSBSqaJQKFEgCMOqy4inOaeTUtNkwyqo5Ht5%2BTMbGQA).

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example in the linked pyright playground or with pyright 1.1.401, then trace tuple unpacking and TypeVarTuple inference. Done means the list is inferred as a list whose elements are the union of the TypeVarTuple entries, while the existing tuple conversion inference remains correct; no source file or regression test is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.