Not detecting `enum.auto` vs `enum.auto()`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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:
- The code doesn't run
- 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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, den Bericht mit den bereitgestellten IntEnum- und StrEnum-Beispielen unter Python 3.12.4 zu reproduzieren, und verfolge anschließend mypys Behandlung von Enum-Mitgliedern und enum.auto, um herauszufinden, wo ein Funktionswert ohne Diagnose akzeptiert wird. Füge Abdeckung für beide Fälle ohne Aufruf hinzu und überprüfe, dass mypy Fehler meldet, während die aufgerufenen auto()-Fälle ihre aktuell aufgelösten Typen behalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100