mypy cannot determine Enum members from static or Final values
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
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 three cases in testing1.py, testing2.py, and testing3.py with mypy 1.10.0, then trace the functional IntEnum and StrEnum analysis that requires a literal mapping. Add regression coverage for Final-valued mappings, including the comprehension case, and confirm mypy infers the enum members without errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100