python / python/mypy

Not detecting `enum.auto` vs `enum.auto()`

Aperta
#17,650 1 commento 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

mypy is not detecting forgetting to call enum.auto() in IntEnum and StrEnum.

To Reproduce

from enum import IntEnum, StrEnum, auto

class SomeIntEnum(IntEnum):
    CORRECT_AUTO = auto()
    INCORRECT_AUTO = auto

reveal_type(SomeIntEnum.CORRECT_AUTO.value)
reveal_type(SomeIntEnum.INCORRECT_AUTO.value)

class SomeStrEnum(StrEnum):
    CORRECT_AUTO = auto()
    INCORRECT_AUTO = auto

reveal_type(SomeStrEnum.CORRECT_AUTO.value)
reveal_type(SomeStrEnum.INCORRECT_AUTO.value)

Running this code will actually crash with a TypeError, but mypy will pass:

  • For the IntEnum.INCORRECT_AUTO: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'type'
  • For the StrEnum.INCORRECT_AUTO: TypeError: <class 'enum.auto'> is not a string

Expected Behavior

I expect mypy to error when one forgets to call auto, as:

  1. The code doesn't run
  2. Even if it did run, the enum's value wouldn't be a primitive as intended

Actual Behavior

mypy passes with the below output:

a.py:7:13: note: Revealed type is "builtins.int"
a.py:8:13: note: Revealed type is "def () -> enum.auto"
a.py:14:13: note: Revealed type is "builtins.str"
a.py:15:13: note: Revealed type is "def () -> enum.auto"

Your Environment

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

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 il report con gli esempi IntEnum e StrEnum forniti su Python 3.12.4, quindi segui la gestione da parte di mypy dei membri enum e di enum.auto per trovare il punto in cui un valore funzione viene accettato senza una diagnostica. Aggiungi la copertura per entrambi i casi senza chiamata e verifica che mypy segnali gli errori, mentre i casi auto() chiamati mantengono i tipi rivelati attuali.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.