tuple element types wrongly mixed as union
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When a variable conditionally gets one or the other of a set of fixed-length tuples, instead of being inferred as a union of said tuple types, mypy sees an arbibrary-length tuple whose elements are typed with a union of all element types involved. This results false-positive errors being reported.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=4780da0a891d33c46ee7ac0b55a05f24
Expected Behavior
Similar to what pyright reveals, a union of two tuple types with defined length and element types, and no error when assigning :
/home/user/omaha/omaha/uitest/mypy-test.py:9:17 - information: Type of "item" is "tuple[Literal[1], Literal['a'], list[bool]] | tuple[Literal[1], Literal['a']]"
Actual Behavior
The type is instead inferred to be a tuple of arbitrary length, with all elements allowed to have any of the types seen inside the tuple:
main.py:9: note: Revealed type is "builtins.tuple[builtins.int | builtins.str | builtins.list[builtins.bool], ...]"
main.py:10: error: List item 0 has incompatible type "tuple[int | str | list[bool], ...]"; expected "tuple[int, str] | tuple[int, str, Sequence[bool]]" [list-item]
Note that when using just item = (1, "a"), removing the conditional part, the tuple is properly identified with fixed types, and the assignment is validated by mypy:
main.py:9: note: Revealed type is "tuple[builtins.int, builtins.str]"
Your Environment
- Mypy version used: 1.18.2
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12, 3.14.0rc2
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo la riproduzione collegata di mypy-play con mypy 1.18.2 e confronta il tipo rilevato e la diagnostica dell’assegnazione con il risultato atteso di pyright. Traccia il comportamento dell’inferenza delle tuple coinvolto nell’assegnazione condizionale; il lavoro è concluso quando le alternative di tuple a lunghezza fissa rimangono un’unione e l’assegnazione mostrata non produce più un falso positivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100