``Optional[Type]`` ignores (implicit) None check, resulting in misstyping of ``Type`` to ``Optional[Type]``
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
It seems that when the check for None is implicit mypy misstype Type to Optional[Type]. I checked #9428 which look really similar, but think the problem is that the control flow in my example is a little convoluted and that the check is not really explicit. (This is pylint's https://github.com/PyCQA/pylint/blob/master/pylint/message/message_id_store.py#L79)
To Reproduce
class MessageIdStore:
def __init__(self):
self.__msgid_to_symbol: Dict[str, str] = {}
self.__symbol_to_msgid: Dict[str, str] = {}
def check_msgid_and_symbol(self, msgid: str, symbol: str) -> None:
existing_msgid: Optional[str] = self.__symbol_to_msgid.get(symbol)
existing_symbol: Optional[str] = self.__msgid_to_symbol.get(msgid)
if existing_symbol is None and existing_msgid is None:
return # both symbol and msgid are usable
if existing_msgid is not None:
if existing_msgid != msgid:
self._raise_duplicate_msgid(symbol, msgid, existing_msgid)
if existing_symbol != symbol:
# This will emit: 'Argument 3 to "_raise_duplicate_symbol" of "MessageIdStore"
# has incompatible type "Optional[str]"; expected "str"'
self._raise_duplicate_symbol(msgid, symbol, existing_symbol)
@staticmethod
def _raise_duplicate_symbol(msgid: str, symbol: str, other_symbol: str) -> None:
...
@staticmethod
def _raise_duplicate_msgid(symbol: str, msgid: str, other_msgid: str) -> None:
...
Actual Behavior
On line self._raise_duplicate_symbol(msgid, symbol, existing_symbol) we have an Argument 3 to "_raise_duplicate_symbol" of "MessageIdStore" has incompatible type "Optional[str]"; expected "str".
Expected Behavior
No error because if not(existing_symbol is None and existing_msgid is None) and not(existing_msgid is not None then we can deduce that existing_symbol is not None
Your Environment
- Mypy version used: v0.812
- Mypy command-line flags: :
["--ignore-missing-imports", "--scripts-are-modules"]
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 reproducer fornito con la versione di mypy e i flag indicati, quindi segui il modo in cui i suoi controlli del flusso di controllo restringono existing_symbol e existing_msgid. Il lavoro è completato quando la chiamata finale a _raise_duplicate_symbol accetta existing_symbol senza un errore Optional[str], mantenendo al contempo i controlli esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100