Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum plugin should support unique check where possible #10858

Open
sobolevn opened this issue Jul 22, 2021 · 2 comments · May be fixed by #11249
Open

Enum plugin should support unique check where possible #10858

sobolevn opened this issue Jul 22, 2021 · 2 comments · May be fixed by #11249

Comments

@sobolevn
Copy link
Member

Feature

After #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

@sobolevn
Copy link
Member Author

And as always, I would love to work on this after #10857 🙂

@sobolevn
Copy link
Member Author

sobolevn commented Oct 2, 2021

Some progress:
Снимок экрана 2021-10-02 в 16 35 03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants