python / python/mypy

``Optional[Type]`` ignores (implicit) None check, resulting in misstyping of ``Type`` to ``Optional[Type]``

Offen
#10,559 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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"]

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den bereitgestellten Reproducer mit der angegebenen mypy-Version und den angegebenen Flags auszuführen, und verfolge dann, wie die Kontrollflussprüfungen existing_symbol und existing_msgid eingrenzen. Als erledigt gilt die Aufgabe, wenn der abschließende Aufruf von _raise_duplicate_symbol existing_symbol ohne einen Optional[str]-Fehler akzeptiert und dabei die bestehenden Prüfungen beibehalten werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.