def typed with Union passed twice is wrongly reported "Incompatible return value type"
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
mypy errantly cites error got "Tuple[Optional[str], Optional[str]]", expected "Union[Tuple[str, str], Tuple[None, None]]". Seems to mix up the Union and Optional.
First, given file test1.py that passes mypy
from typing import Union, Tuple
def def1(var: bool) -> Union[Tuple[str, str], Tuple[None, None]]:
a, b = "", ""
if var:
return a, b
return None, None
Run command
python -m mypy test1.py
(test1.py passes).
Then add a call to another function with the same type declaration.
Given file test2.py
from typing import Union, Tuple
def def1(var: bool) -> Union[Tuple[str, str], Tuple[None, None]]:
if var:
return "", ""
return None, None
def def2(var: bool) -> Union[Tuple[str, str], Tuple[None, None]]:
a, b = def1(var)
if var:
return a, b
return None, None
The same mypy command fails test2.py
python -m mypy test2.py
test2.py:8: error: Incompatible return value type (got "Tuple[Optional[str], Optional[str]]", expected "Union[Tuple[str, str], Tuple[None, None]]")
Found 1 error in 1 file (checked 1 source file)
Environment
- Mypy version used: mypy 0.812
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.8.5
- Operating system and version: Windows 10 Pro
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
Führen Sie python -m mypy test2.py aus und vergleichen Sie das Ergebnis mit test1.py, wobei Sie sich auf den Typprüfpfad für das Entpacken des Ergebnisses Union[Tuple[str, str], Tuple[None, None]] in def2 konzentrieren. Erledigt ist die Aufgabe, wenn test2.py nicht mehr den falschen Fehler „incompatible-return-value“ meldet und test1.py weiterhin erfolgreich ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100