This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Provide enum.unique class decorator
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
Status: closed Resolution: fixed
Dependencies: 17947 Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, ezio.melotti, ncoghlan, pconnell, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2013-05-23 08:56 by ncoghlan, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
unique.stoneleaf.01.patch ethan.furman, 2013-06-28 08:49 review
unique.stoneleaf.02.patch ethan.furman, 2013-06-29 18:23 review
Messages (14)
msg189854 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-05-23 08:56
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 !=
                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
msg189881 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-05-23 22:23
This is certainly an effective method, but it places safety off by default.  I would rather have a system that was from duplicates by default but had an easy override.

The method I had in place in my original code was something like:

class Color(Enum, options=DUPLICATES):
    red = 1
    green = 2
    blue = 3
    grene = 2

Without the DUPLICATES option, the above class would raise an error.  Safe(r) by default, easy override.

If my suggestion doesn't fly, we should definitely put Nick's in.
msg189883 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-05-23 22:38
I take Guido's acceptance of the PEP (and the discussion in the previous
issue) as meaning the default behaviour (allowing aliases) is no longer up
for debate. Hence this suggestion to offer a self-documenting way to opt in
to the more restrictive variant.
msg189884 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-05-23 22:43
I'm not giving up hope yet.  Plenty of Python features no longer work the way they did when their PEP was accepted.  ;)
msg189890 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-05-24 02:53
You don't generally see reversals of decisions where Guido has made an explicit choice based on consistency with the rest of the language. The fact that aliases are permitted in enumerations by default is consistent with the normal behaviour of namespaces and dictionaries in general, so providing a way to opt in to the stricter checks is a better solution.

The idea of passing flags or other configuration options to the metaclass is also rather ugly. Offering permissive behaviour by default with an easy way to opt in to additional restrictions is far more in keeping with the general "consenting adults" ethos of the language.
msg189892 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-05-24 04:18
Oh.  Well, I like your decorator. :)
msg189894 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-05-24 05:07
Don't worry, compared to some of the ideas I've had (and rightfully had shot down) over the years, that one was positively sensible :)
msg190807 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2013-06-08 16:38
+1 for the decorator!
msg191450 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-06-19 06:49
I haven't seen any discouraging words regarding the decorator.  If no one has any compelling reasons why it shouldn't be added, I'll craft a version and put it in (only real difference with Nick's would be catching all the duplicates at once instead of one at a time).
msg191982 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-06-28 08:49
unique() added to; tests added; docs updated.

If no complaints within a few days I'll push them through.
msg191992 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-06-28 12:51
Sent some review comments. I'll be on a short vacation this weekend, so please wait at least until next week so I can review the changes. Also, Nick should definitely review this too :)
msg192041 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-06-29 18:23
Integrated comments.
msg192165 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-07-02 00:12
The documentation still contains an "Interesting example": UniqueEnum. I would prefer to only have one obvious way to get unique enum, so please just drop this example. Or at least, mention the new decorator in the example.
msg193340 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-07-19 00:05
New changeset 2079a517193b by Ethan Furman in branch 'default':
closes issue18042 -- a `unique` decorator is added to
Date User Action Args
2022-04-11 14:57:45adminsetgithub: 62242
2013-07-19 00:05:56python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg193340

resolution: fixed
stage: patch review -> resolved
2013-07-02 00:12:36vstinnersetnosy: + vstinner
messages: + msg192165
2013-06-29 18:23:42ethan.furmansetfiles: + unique.stoneleaf.02.patch

messages: + msg192041
2013-06-28 12:51:19eli.benderskysetmessages: + msg191992
2013-06-28 08:49:57ethan.furmansetstage: patch review
2013-06-28 08:49:32ethan.furmansetfiles: + unique.stoneleaf.01.patch
keywords: + patch
messages: + msg191982
2013-06-19 06:49:11ethan.furmansetassignee: ethan.furman
messages: + msg191450
2013-06-08 16:38:03barrysetmessages: + msg190807
2013-06-08 16:37:05barrysetnosy: + barry
2013-05-26 13:42:20ezio.melottisetnosy: + ezio.melotti
2013-05-25 09:31:07pconnellsetnosy: + pconnell
2013-05-24 05:07:30ncoghlansetmessages: + msg189894
2013-05-24 04:18:12ethan.furmansetmessages: + msg189892
2013-05-24 02:53:23ncoghlansetmessages: + msg189890
2013-05-23 22:43:41ethan.furmansetmessages: + msg189884
2013-05-23 22:38:31ncoghlansetmessages: + msg189883
2013-05-23 22:23:35ethan.furmansetmessages: + msg189881
2013-05-23 08:56:55ncoghlansetnosy: + eli.bendersky, ethan.furman
dependencies: + Code, test, and doc review for PEP-0435 Enum
2013-05-23 08:56:18ncoghlancreate