Mypy don't narrow types of 2 variables based on its combination
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
Mypy don't narrow types of 2 variables based on its combination.
x: Literal["a", "b"] = a_or_b()
y: Literal["a", "b"] = a_or_b()
if not (x == "a" and y == "a") and x == "a":
# At this point, the type of y should be Literal["b"],
# but mypy reports "Union[Literal['a'], Literal['b']]"
reveal_type(y)
Note: the above if block can be separated into 2, which is probably more likely scenario in real codebase, such that:
if x == "a" and y == "a":
# some code...
pass
elif x == "a":
# At this point, the type of y should be Literal["b"],
# but mypy reports "Union[Literal['a'], Literal['b']]"
reveal_type(y)
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=9d0b24fba42e824b220e087003559068
Expected Behavior
Mypy narrows the type of y to be Literal["b"]
Actual Behavior
Mypy doesn't narrow the type and reports the type of y to be "Union[Literal['a'], Literal['b']]"
Your Environment
- Mypy version used: 1.7.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.0
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 con il riproduttore mypy-play collegato usando Mypy 1.7.1 e Python 3.12, quindi confronta le due forme condizionali e il relativo output di reveal_type. Il lavoro è completato quando il secondo ramo restringe y da Literal["a", "b"] a Literal["b"] senza introdurre regressioni nel comportamento esistente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100