python / python/mypy

[1.19 regression] Incorrect return type inference on enum methods

Offen
#20,434 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-enum topic-inference topic-type-narrowing
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Typing on a class method on a list now fails, and the suggested fix still doesn't work. Gists are best way to show this.

(A clear and concise description of what the bug is.)

To Reproduce

Gist URL: https://gist.github.com/mypy-play/86bb1ad0946410002b776f162c0e075c
Playground URL: https://mypy-play.net/?mypy=1.19.0&python=3.13&gist=86bb1ad0946410002b776f162c0e075c

Gist has three methods, get_levels_1 get_levels_2 and get_levels_3. get_levels_1 is how i previously had the code on 1.18.2, and it gets this error

main.py:32: error: Incompatible return value type (got "list[Self]", expected "list[Level]")  [return-value]
main.py:32: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:32: note: Consider using "Sequence" instead, which is covariant
main.py:32: note: Perhaps you need a type annotation for "levels"? Suggestion: "list[Level]"

When changing return type to list[Level] (get_levels_2) you get this error

main.py:32: error: Incompatible return value type (got "list[Self]", expected "list[Level]")  [return-value]
main.py:32: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:32: note: Consider using "Sequence" instead, which is covariant
main.py:32: note: Perhaps you need a type annotation for "levels"? Suggestion: "list[Level]"

Only typing the levels argument works (get_levels_3)

Expected Behavior

On 1.18.2 get_levels_1 works fine and this regresses in 1.19.0

Your Environment

Mypy version used: 1.19.0
Mypy command-line flags: n/a
Mypy configuration options from mypy.ini (and other config files):

[mypy]
plugins = pydantic.mypy
ignore_missing_imports = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
warn_unused_ignores = True
warn_no_return = True
warn_return_any = True
warn_redundant_casts = True
strict_equality = True

Python version used: 3.13.2

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 verknüpften gist oder das mypy-play-Beispiel mit mypy 1.19.0 auszuführen und mit 1.18.2 zu vergleichen, wobei du dich auf die Methoden get_levels_1, get_levels_2 und get_levels_3 konzentrierst. Verfolge die Inferenz des Rückgabetyps der enum-Methode und füge einen Regressionstest hinzu, der zeigt, dass die zuvor gültige Annotation keinen Fehler mehr erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
50/100

Neue Issues direkt in Ihr Postfach

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