Failure to narrow down type of elements of Tuple
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
-
Are you reporting a bug, or opening a feature request?
kind of both -
Please insert below the code you are checking with mypy,
from typing import Optional, Tuple, TypeVar
_T = TypeVar("_T")
_U = TypeVar("_U")
def promote_optional(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
for val in tup:
if val is None:
return None
return tup
def promote_optional2(tup: Tuple[Optional[_T], Optional[_U]]) -> Optional[Tuple[_T, _U]]:
if tup[0] is None:
return None
if tup[1] is None:
return None
return tup
- What is the actual behavior/output?
/tmp/mwe.py:11: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
/tmp/mwe.py:19: error: Incompatible return value type (got "Tuple[Optional[_T], Optional[_U]]", expected "Optional[Tuple[_T, _U]]")
Found 2 errors in 1 file (checked 1 source file)
-
What is the behavior/output you expect?
Mypy should accept the code. -
What are the versions of mypy and Python you are using?
mypy 0.782
I want to turn a Tuple[Optional...] to an Optional[Tuple], because the whole tuple is worthless if it contains any None. Unfortunately Mypy seems to be unable to detect that I checked all elements of the tuple for being none.
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
Beginne mit der minimalen Reproduktion in /tmp/mwe.py und führe sie mit mypy aus, um die aktuellen Fehler zu bestätigen. Verfolge, wie mypy die Typen von Tupel-Elementen nach den None-Prüfungen eingrenzt; abgeschlossen ist die Arbeit, wenn beide promote_optional-Funktionen ohne Fehler aufgrund inkompatibler Rückgabetypen akzeptiert werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100