Support enum member `value` types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Not sure if this would be in mypy or the typeshed, though it may need support in both.
Currently enum.IntEnum provides a way to have an enum whose value is an int, however all other enum.Enums have a .value of Any.
While this can be worked around in more recent versions of Python by adding a member without a value within the enum:
class MyEnum(str, enum.Enum):
value: str
FIRST = 'first'
This isn't ideal as it means that there are two places where this needs to be specified -- as an inherited type and within the class body.
It feels like it would be better if mypy was able to infer the member value type from the inheritance.
Aside: had generics existed before Enum, it feels like that might alternatively solved this, something like:
class MyEnum(enum.Enum[str]):
FIRST = 'first'
Edit:
Note that this issue is about the general use-case of variables typed for the enum (such as x: MyEnum) -- mypy has long supported determining the type of specific members and their .values (e.g: reveal_type(MyEnum.FIRST.value) is already str or more recently Literal['first']).
I've put a more complete demo of this at https://gist.github.com/PeterJCLaw/d4334e498ab1391ee306886748e675f1.
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 reviewing the issue's complete demo and tracing enum handling in mypy and typeshed; the issue does not identify specific files or tests. Determine how an enum variable's value type should be inferred from inheritance, then add coverage showing that general enum instances expose the intended value type without a separate class-body annotation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100