python / python/mypy

Type fails to get "refined" on branch when `is None`-check happens in variable assignment

Aperta
#9,229 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature priority-2-low topic-pep-572 topic-type-narrowing
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? Bug
  • Please insert below the code you are checking with mypy:
from typing import Optional

def bar(var: str):
    pass

def foo(var: Optional[str]=None):
    var_not_None = not (var is None)
    if var_not_None:
        # var can only be a `str` at this point,
        # but `mypy` complains that
        # Argument 1 to "bar" has incompatible type "Optional[str]"; expected "str"
        bar(var)

def foo2(var: Optional[str]=None):
    if not(var is None):
        # no complaints from mypy
        bar(var)
  • What is the actual behavior/output?

Running mypy on this reproducer returns:

error: Argument 1 to "bar" has incompatible type "Optional[str]"; expected "str" Found 1 error in 1 file (checked 1 source file)

  • What is the behavior/output you expect?

No errors, both foo2 and foo are equivalent.

  • What are the versions of mypy and Python you are using?

mypy 0.782 and Python 3.6.8

  • Do you see the same issue after installing mypy from Git master?

Yup, unless I did something wrong 😅

Summary:

When you have an Optional[str]-typed variable var, one would expect that an not (var is None) check in an if-statement should tell mypy that all code within that branch has that the variable has type str. Indeed, mypy shows the expected behaviour for this, as shown in function foo2 above.

However, there are many instances where you want to check if a variable is None outside of the if-statement. For instance, if your if-statement were to check many boolean conditions at once. In this situation, one may save the result of the check into a variable. This is the case in the foo function above, where mypy complains.

I suspect this problem may not be solvable in the general case, but perhaps we can improve the status-quo nonetheless to catch this error.

If this is a beginner-friendly bug, I would love to help fix it! I've been meaning to get involved with the mypy project; just some guidance would do. Thanks!

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

Non sono indicati né un file sorgente né un test. Inizia eseguendo il riproduttore Python fornito e confronta il restringimento del tipo in foo con quello in foo2; l'attività è completata quando il controllo di None basato sull'assegnazione non produce alcun errore incompatible-type e preserva il comportamento esistente.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.