python / python/mypy

def typed with Union passed twice is wrongly reported "Incompatible return value type"

Aperta
#10,225 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esegui python -m mypy test2.py e confrontalo con test1.py, concentrandoti sul percorso di controllo dei tipi per l'estrazione tramite unpacking del risultato Union[Tuple[str, str], Tuple[None, None]] in def2. Il lavoro è completato quando test2.py non segnala più l'errore errato „incompatible-return-value“ e test1.py continua a superare il controllo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.