Failure to narrow down type of elements of Tuple
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
-
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.
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 la riproduzione minima in /tmp/mwe.py ed eseguila con mypy per confermare gli errori attuali. Traccia come mypy restringe i tipi degli elementi della tupla dopo i controlli su None; il lavoro è completato quando entrambe le funzioni promote_optional vengono accettate senza errori di tipo restituito incompatibili.
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
- 35/100