microsoft / microsoft/pyright

1.1.365 regression: tuples created from iterating over a tuple of literals looses literal type information

Open
#11,146 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**
A clear and concise description of the behavior you are seeing and the expected behavior along with steps to reproduce it.

Prior to https://github.com/microsoft/pyright/releases/tag/1.1.365, creating tuples when iterating over a list of literal strings would have the loop variable be a union of all literals.

**Code or Screenshots**
If possible, provide a minimal, self-contained code sample (surrounded by triple back ticks) to demonstrate the issue. The code should define or import all referenced symbols.

```python
from typing import Literal, reveal_type

Hotkey = Literal[
"split",
"reset",
"skip_split",
"undo_split",
"pause",
"screenshot",
"toggle_auto_reset_image",
]
HOTKEYS = (
"split",
"reset",
"skip_split",
"undo_split",
"pause",
"screenshot",
"toggle_auto_reset_image",
)

def some_function_taking_a_hotkey(hotkey: Hotkey): ...

# Case 1
for hotkey1, hotkey_name in ((hotkey, f"{hotkey}_hotkey") for hotkey in HOTKEYS):
# ...
reveal_type(hotkey1)
some_function_taking_a_hotkey(hotkey1)

# Case 2
for hotkey2 in HOTKEYS:
# ...
reveal_type(hotkey2)
some_function_taking_a_hotkey(hotkey2)
```

mypy doesn't understand either cases (revealed type `str`)
ty (0.0.1-alpha.29) understands both cases
pyright 1.1.407 only understands the second case
pyright 1.1.364 correctly identifies the revealed type as `Literal['split', 'reset', 'skip_split', 'undo_split', 'pause', 'screenshot', 'toggle_auto_reset_image']` in both cases.

**VS Code extension or command-line**
(see above mentioned versions)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python reproducer and compare its revealed types between pyright 1.1.364, 1.1.365, and 1.1.407. Trace tuple and generator type inference for Case 1; done means the loop variable retains the full Literal union in both cases without regressing Case 2.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.