and expression with bool() does not work with Optionals
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
For the following code, mypy reports an incompatible type error on the function f
from typing import Optional
def f(x:Optional[str]) -> bool:
return bool(x) and g(x)
def f2(x:Optional[str]) -> bool:
if x:
return g(x)
return False
def g(x: str) -> bool:
return bool(x)
test.py:4: error: Argument 1 to "g" has incompatible type "Optional[str]"; expected "str"
however the code is equivalent to the one in f2, for which no error is raised.
-
What are the versions of mypy and Python you are using?
Mypy: 0.620, Python: 3.6.5 -
What are the mypy flags you are using? (For example --strict-optional)
[mypy]
# This flag should be removed here and enabled per module when we have a
# considerable number of stubs for external libraries.
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True
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 il riproduttore minimo segnalato con strict_optional abilitato e confronta le diagnosi per f e f2. Traccia la gestione da parte di mypy delle espressioni bool(x) e g(x) e il narrowing di Optional[str]. Il lavoro è completato quando la funzione f equivalente viene accettata senza l'errore incompatible-type, mentre il comportamento esistente di f2 rimane valido.
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
- Specificata chiaramente
- Idoneità per principianti
- 42/100