python / python/mypy

mypy cannot determine Enum members from static or Final values

Aperta
#17,187 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report
To Reproduce

Here are 3 variations on creating IntEnum or StrEnum using functional API syntax.

testing1.py: (Success)

UpperSeat = IntEnum("UpperSeat", {"Seat1": 1, "Seat2": 2, "Seat3": 3})

testing2.py: (Error)

UPPER_CODES: Final[dict[str, int]] = {"Seat1": 1, "Seat2": 2, "Seat3": 3}
UpperSeat = IntEnum("UpperSeat", UPPER_CODES)

testing3.py: (Error)

UPPER_CODES: Final[dict[str, int]] = {f"Seat{code}": code for code in range(1, 3)}
UpperSeat = IntEnum("UpperSeat", UPPER_CODES)

Expected Behavior

All three of these produce valid Python code, but only one of these passes mypy type analysis.
I would argue that UPPER_CODES in both testing2 and testing3 are static.
In testing2.py, UPPER_CODES is a static constant.
In testing3.py, UPPER_CODES is provably static.
In both testing2 and testing3, Final values are used, so mypy should not be throwing this error.

Actual Behavior

testing1.py - Uses static values in-line: mypy yields Success
testing2.py and testing3.py: mypy yields Error:

main.py:8: error: Second argument of IntEnum() must be string, tuple, list or dict literal for mypy to determine Enum members  [misc]
main.py:9: error: Second argument of StrEnum() must be string, tuple, list or dict literal for mypy to determine Enum members  [misc]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: python 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 i tre casi in testing1.py, testing2.py e testing3.py con mypy 1.10.0, quindi traccia l’analisi funzionale di IntEnum e StrEnum che richiede una mappatura letterale. Aggiungi una copertura di regressione per le mappature con valori Final, incluso il caso di comprehension, e conferma che mypy inferisca i membri dell’enum senza errori.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.