mypy can not correctly tell when optional type can not be None
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
Bug Report
mypy can not correctly tell when optional type can not be None if the variable was previously set or checked in @property.
To Reproduce
class Test(object):
def __init__(self) -> None:
self.test: str | None = None
@property
def is_set(self) -> bool:
return self.test is not None
def failed_check(self) -> None:
if not self.is_set:
return
# self.test can not be None
value = self.test.strip(',')
print(value)
class Test2(object):
def __init__(self) -> None:
self.test: str | None = None
def set_me(self) -> None:
self.test = 'test'
def use_me(self) -> None:
self.set_me()
# self.test can not be None
self.test.strip(',')
Expected Behavior
No issues found.
Actual Behavior
test.py:14: error: Item "None" of "Optional[str]" has no attribute "strip" [union-attr]
test.py:28: error: Item "None" of "Optional[str]" has no attribute "strip" [union-attr]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 0.991 (compiled: yes)
- Mypy command-line flags:
mypy test.py - Mypy configuration options fro m
mypy.ini(and other config files): - Python version used: Python 3.10.8
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 salvando gli esempi come test.py ed eseguendo mypy test.py con le versioni indicate di Python e mypy. Traccia come mypy restringe i valori Optional dopo i controlli delle proprietà e le chiamate ai metodi; il lavoro è completato quando entrambi gli esempi non producono errori e c'è una copertura di regressione per il comportamento segnalato.
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