Ignore incompatible type error when variable is checked in if statement
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
I'm using mypy for type checking in my Python projects and I've come across a situation where I believe mypy could be improved.
Consider the following code:
def foo1(a: str): ...
def foo2(a: str | None, b: str | None):
if not a and not b:
return None
if not a:
return foo1(b)
return None
In this case, mypy raises an error:
Argument 1 to "foo1" has incompatible type "str | None"; expected "str" [arg-type].
However, the function foo1 is only called when b
is not None, so in practice, there is no type error.
The logic of the function ensures that b cannot be None when foo1(b) is called.
This is because in the first if statement, we check if both a and b are None.
If they are not, we proceed to the next if statement where we check if a is None.
If a is None, then b must not be None because of the previous check.
Therefore, foo1(b) is only called when b is not None.
Versions
Python: 3.10.10
MyPy: 1.7.1
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 eseguendo mypy sul riproduttore Python fornito e traccia il modo in cui le due istruzioni if influiscono sul restringimento dei tipi. Individua i test rilevanti per il controllo dei tipi o l’entry point, quindi aggiungi un caso di regressione per questo flusso di controllo. Il lavoro è completato quando mypy accetta foo1(b) senza un errore arg-type, mantenendo al contempo i controlli esistenti.
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