Mypy don't narrow types of 2 variables based on its combination
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem verknüpften mypy-play-Reproduzierer unter Verwendung von Mypy 1.7.1 und Python 3.12 und vergleichen Sie dann die beiden bedingten Formen und ihre reveal_type-Ausgabe. Als abgeschlossen gilt die Aufgabe, wenn der zweite Zweig y von Literal["a", "b"] auf Literal["b"] eingrenzt, ohne das bestehende Verhalten zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100