Not detecting `enum.auto` vs `enum.auto()`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the report with the provided IntEnum and StrEnum examples on Python 3.12.4, then trace mypy's enum member and enum.auto handling to find where a function value is accepted without a diagnostic. Add coverage for both missing-call cases and verify that mypy reports errors while the called auto() cases retain their current revealed types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100