python / python/mypy

Incompatible return value type `type[Enum]` when returning a functional-style Enum

Aperta
#17,147 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report
When returning a functional-style Enum class from a function with the return type type[Enum] mypy reports an error.

When the created Enum class is assigned to a variable and the variable is return, mypy does not report an error.

To Reproduce

from enum import Enum

def direct_return() -> type[Enum]:
    return Enum("direct", {"a": 0})    # error: Incompatible return value type (got "Enum", expected "type[Enum]")  [return-value]

def indirect_return() -> type[Enum]:
    indirect = Enum("indirect", {"a": 0})
    return indirect                    # no error as expected

Gist URL
Playground URL

Expected Behavior

no mypy warnings

Actual Behavior

main.py:4: error: Incompatible return value type (got "Enum", expected "type[Enum]") [return-value]

Your Environment

  • Mypy version used: 1.9.0 and master on mypy-play.net
  • Mypy command-line flags: mypy-play.net defaults
  • Mypy configuration options from mypy.ini (and other config files): mypy-play.net defaults
  • Python version used: 3.12

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 riproducendo gli esempi direct_return e indirect_return con mypy 1.9.0 o master, usando come riferimento il comportamento del playground collegato. Traccia il modo in cui le chiamate a Enum in stile funzionale vengono inferite nel type checker, quindi aggiungi un test di regressione che dimostri che entrambe le funzioni restituiscono senza avvisi.

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à
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.