`Enum` plugin should support `unique` check where possible
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
After https://github.com/python/mypy/pull/10852 is merged we can work on making @enum.unique to check for actually unique values.
Right now it does nothing. In other words, this code passes mypy check:
from enum import Enum, unique
@unique
class A(Enum):
x = 1
y = 1
But, fails in runtime:
Traceback (most recent call last):
File "ex.py", line 4, in <module>
class A(Enum):
File "/Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/enum.py", line 969, in unique
raise ValueError('duplicate values found in %r: %s' %
ValueError: duplicate values found in <enum 'A'>: y -> x
But, since after #10852 all fields would be implicitly final and their values will be inferred as Literal types, this gives us some space to check that we have unique literal values inside mypy/plugins/enums.py
Corner case
We should ignore any non-Literal values from this check:
def rand_int() -> int:
...
@unique
class A(Enum):
x = rand_int()
y = rand_int()
z = 0
This can even raise in runtime if x or y is 0, but we have no way of knowing this in advance.
Related #5599
Related #10857
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
After confirming the prerequisite work in #10852, start in mypy/plugins/enums.py and inspect how Enum field values are inferred as Literal types. Implement the @enum.unique check for duplicate Literal values while ignoring non-Literal values, then verify the examples and corner case described in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100