python / python/mypy

Not reporting any error when function returns bool type while expecting str type

Offen
#9,194 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Hi

I noticed a situation where I wasn't getting a type error from mypy when I would have expected to get one.

Below is the code I have. I purposefully set the return type to str because I wanted to see a mypy error, and specifically I expected the error to be that I had set the wrong return type (e.g. as the code is doing a comparison, which will thus return a bool type, I expected mypy to say the return type should be of type bool):

def is_dev() -> str:
    return settings.get("environment") == "dev"

In that code snippet you'll see settings.get(...), which is a function that returns a string, and we compare that string against another string.

Note: as settings.get is custom code, mypy knows nothing about what type is being returned.

My hypothesis is that because my specific example uses code that mypy can't identify the type for (i.e. settings.get()), mypy just goes ahead and ignores any other possible type check errors (such as me specifying the return type as str).

If that's expected of mypy then fine, but it just confused me.

I'm using mypy version 0.782 and Python version 3.7.7.

I'm running mypy in two places:

  1. vim integration (e.g. mypy --ignore-missing-imports --strict-equality)
  2. tox (e.g. mypy --ignore-missing-imports --package foo)

Note: I've tried also changing to --no-strict-equality and it makes no difference.

Thank you for any guidance you can give me.

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

Reproduziere die gemeldete Funktion mit mypy 0.782, Python 3.7.7 und den aufgeführten Optionen --strict-equality und --ignore-missing-imports und vergleiche anschließend die Diagnosen mit dem erwarteten Fehler für den Rückgabetyp. Verfolge den Typprüfpfad für Rückgabeausdrücke und unbekannte Typen; als erledigt gilt die Aufgabe, wenn das Verhalten korrigiert oder eindeutig mit einem Regressionstest dokumentiert ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
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.