python / python/mypy

[1.19 regression] Incorrect return type inference on enum methods

Aperta
#20,434 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-enum topic-inference topic-type-narrowing
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il gist collegato o l’esempio mypy-play con mypy 1.19.0 e confrontalo con 1.18.2, concentrandoti sui metodi get_levels_1, get_levels_2 e get_levels_3. Traccia l’inferenza del tipo restituito dal metodo enum e aggiungi un test di regressione che mostri che l’annotazione precedentemente valida non produce più un errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
50/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.