Enum `@member` and `nonmember` mishandled in `Literal` types
Open
Nobody has claimed this yet.
bug
topic-enum
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former.
To Reproduce
from enum import Enum, member, nonmember
from typing import Literal
class E(Enum):
A = 1
@member
def B() -> None: ...
C = member(lambda: None)
D = nonmember(1)
def meth(self) -> None: ...
a: Literal[E.A] # OK, true negative
b: Literal[E.B] # False positive: Parameter 1 of Literal[...] is invalid [valid-type]
c: Literal[E.C] # OK, true negative
d: Literal[E.D] # False negative - `nonmember` shouldn't be allowed
meth: Literal[E.meth] # OK, true positive: Parameter 1 of Literal[...] is invalid [valid-type]
Expected Behavior
- The following should be accepted in Literal:
E.A,E.B,E.C - The following should be rejected in Literal:
E.D,E.meth
Actual Behavior
(line 7 is a different issue reported separately)
main.py:7: error: Method must have at least one argument. Did you forget the "self" argument? [misc]
main.py:14: error: Parameter 1 of Literal[...] is invalid [valid-type]
main.py:17: error: Parameter 1 of Literal[...] is invalid [valid-type]
Your Environment
- Mypy version used: 1.15.0 and current master
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 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 running the supplied Python 3.12 reproduction with mypy 1.15.0 or current master, then trace the Literal validation and enum-member handling involved in those diagnostics. Done means E.A, E.B, and E.C are accepted while E.D and E.meth are rejected.
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
- 45/100