Slow Typechecking 50-element homogeneous tuple
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
Slow Typechecking 50-element homogeneous tuple
**Code or Screenshots**
```python
US_STATES: tuple[tuple[str, str], ...] = (
("AL", "Alabama"),
("AK", "Alaska"),
("AZ", "Arizona"),
("AR", "Arkansas"),
("CA", "California"),
("CO", "Colorado"),
("CT", "Connecticut"),
("DE", "Delaware"),
("DC", "District of Columbia"),
("FL", "Florida"),
("GA", "Georgia"),
("HI", "Hawaii"),
("ID", "Idaho"),
("IL", "Illinois"),
("IN", "Indiana"),
("IA", "Iowa"),
("KS", "Kansas"),
("KY", "Kentucky"),
("LA", "Louisiana"),
("ME", "Maine"),
("MD", "Maryland"),
("MA", "Massachusetts"),
("MI", "Michigan"),
("MN", "Minnesota"),
("MS", "Mississippi"),
("MO", "Missouri"),
("MT", "Montana"),
("NE", "Nebraska"),
("NV", "Nevada"),
("NH", "New Hampshire"),
("NJ", "New Jersey"),
("NM", "New Mexico"),
("NY", "New York"),
("NC", "North Carolina"),
("ND", "North Dakota"),
("OH", "Ohio"),
("OK", "Oklahoma"),
("OR", "Oregon"),
("PA", "Pennsylvania"),
("RI", "Rhode Island"),
("SC", "South Carolina"),
("SD", "South Dakota"),
("TN", "Tennessee"),
("TX", "Texas"),
("UT", "Utah"),
("VT", "Vermont"),
("VA", "Virginia"),
("WA", "Washington"),
("WV", "West Virginia"),
("WI", "Wisconsin"),
("WY", "Wyoming"),
)
US_TERRITORIES: tuple[tuple[str, str], ...] = (
("AS", "American Samoa"),
("GU", "Guam"),
("MP", "Northern Mariana Islands"),
("PR", "Puerto Rico"),
("VI", "Virgin Islands"),
)
STATE_CHOICES: tuple[tuple[str, str], ...] = tuple(sorted((*US_STATES, *US_TERRITORIES), key=lambda obj: obj[1]))
```
```
uv init tmp
cd tmp
cat > states.py # (^^paste above^^, then press enter, then ctrl+d to end.)
uv add pyright[nodejs]
uv run pyright --verbose --stats
```
View full verbose output
```
Loading pyproject.toml file at /home/collin/tmp/pyproject.toml
Pyproject file "/home/collin/tmp/pyproject.toml" has no "[tool.pyright]" section.
No include entries specified; assuming /home/collin/tmp
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Assuming Python version 3.13.2.final.0
Execution environment: python
Extra paths:
(none)
Python version: 3.13.2.final.0
Python platform: Linux
Search paths:
/home/collin/tmp/.venv/lib/python3.13/site-packages/pyright/dist/dist/typeshed-fallback/stdlib
/home/collin/tmp
/home/collin/tmp/typings
/home/collin/tmp/.venv/lib/python3.13/site-packages/pyright/dist/dist/typeshed-fallback/stubs/...
/home/collin/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13
/home/collin/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/lib-dynload
/home/collin/tmp/.venv/lib/python3.13/site-packages
Found 2 source files
pyright 1.1.400
0 errors, 0 warnings, 0 informations
Completed in 0.948sec
Analysis stats
Total files parsed and bound: 23
Total files checked: 2
Timing stats
Find Source Files: 0sec
Read Source Files: 0sec
Tokenize: 0.06sec
Parse: 0.08sec
Resolve Imports: 0.03sec
Bind: 0.06sec
Check: 0.41sec
Detect Cycles: 0sec
```
```
Analysis time by file
325ms: file:///home/collin/tmp/states.py
140ms: file:///home/collin/tmp/main.py
```
Changing `US_STATES: tuple[tuple[str, str], ...] = (` to `US_STATES: list[tuple[str, str]] = [` is 3.5x faster:
```
Analysis time by file
139ms: file:///home/collin/tmp/main.py
90ms: file:///home/collin/tmp/states.py
```
Python tuples are generally slightly faster than lists, especially during import, and they have the benefit of being immutable, but it's surprising that they're way way slower when type checking. (It shouldn't take a third of a second to typecheck just this file.)
(Even `US_STATES: Sequence[tuple[str, str]] = (` is super slow.)
**VS Code extension or command-line**
command-line `pyright 1.1.400`
Contributor guide
Research direction
Reproduce the report in states.py and main.py using the shown uv and pyright --verbose --stats commands, then compare the tuple, list, and Sequence timings. Start by examining the type-checking path responsible for these literals; done means the tuple example type-checks without the disproportionate delay while retaining the reported zero-error result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- performance, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100