python / python/mypy

Misleading issue text when return type doesn't match type annotation

Offen
#14,773 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic-type-context topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When you return a function call like return list(...), return dict(...), and the type doesn't match the return annotation, the error message says that list(...) is not valid code.

To Reproduce

def foo() -> list[int]:
    tup = ("a", "b", "c")
    return list(tup)

Expected Behavior

I was expecting mypy to tell me that the type annotation doesn't match the return type.

Instead it says that the arguments to list() are wrong, i.e. list doesn't take this type.

Like so:

$ mypy asd.py
asd.py:3: error: Incompatible return value type (got "List[str]", expected "List[int]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Actual Behavior

$ mypy asd.py 
asd.py:3: error: Argument 1 to "list" has incompatible type "Tuple[str, str, str]"; expected "Iterable[int]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I think this is misleading.

Your Environment

  • Mypy version used: v1.0.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.9

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 Diagnose mit dem Beispiel in asd.py unter Verwendung der gemeldeten mypy-Version und des gemeldeten Befehls. Verfolge, wie der Rückgabeausdruck und seine Annotation geprüft werden, und sorge dann dafür, dass die Diagnose den inkompatiblen Rückgabewert identifiziert, und füge Abdeckung für die Fälle list(...) und dict(...) hinzu.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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