Skip to content

Duplicate fields in Enum must raise an error #11248

Description

@sobolevn

I have this example:

fromenumimportEnumclassA(Enum):
x=1x=2reveal_type(A.x)

Mypy is happy with it:

» mypyex.pyex.py:8: note: Revealedtypeis"Literal[ex.A.x]?"

But, it raises in runtime:

» pythonex.pyTraceback (mostrecentcalllast):
File"/Users/sobolev/Desktop/mypy/ex.py", line4, in<module>classA(Enum):
File"/Users/sobolev/Desktop/mypy/ex.py", line6, inAx=2File"/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/enum.py", line99, in__setitem__raiseTypeError('Attempted to reuse key: %r'%key)
TypeError: Attemptedtoreusekey: 'x'

This is a part of my "better enum" series:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions