New submission from Nick Coghlan:

Another attempt at tackling the "but I want to ensure my enum values are 
unique" problem that PEP 435 deliberately chose not to handle. My previous 
suggestion (in issue 17959) was rightly rejected due to the other problems it 
caused, but this idea is much cleaner and simpler.

All we would need to do is provide the following class decorator in the enum 
module:

    def unique(new_enum):
        for name, member in new_enum.__members__.items():
            if name != member.name:
                msg = "Alias {!r} for {!r} not permitted in unique Enum"
                raise TypeError(msg.format(name, member))
        return new_enum

Used as so:

>>> @enum.unique
... class MyEnum(enum.Enum):
...     a = 1
...     b = 2
...     c = 1
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 6, in unique
TypeError: Alias 'c' for <MyEnum.a: 1> not permitted in unique Enum

----------
components: Library (Lib)
messages: 189854
nosy: ncoghlan
priority: normal
severity: normal
status: open
title: Provide enum.unique class decorator
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18042>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to