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.

classification
Title: Exception raised when attempting to create Enum via functional API
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, iritkatriel, kamilturek, suhailsingh247
Priority: normal Keywords:

Created on 2021-03-08 00:58 by suhailsingh247, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py suhailsingh247, 2021-03-08 00:58 Failing test case for Enum's functional API when inheriting from custom Enum
Messages (6)
msg388255 - (view) Author: Suhail S. (suhailsingh247) Date: 2021-03-08 00:58
It is possible to create custom Enum classes with a metaclass that is a subtype of EnumMeta. It is also possible to inherit from such an enumeration to create another enumeration. However, attempting to do so via the functional API raises an exception. See attached file that highlights minimal failing test case.
msg390786 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-04-11 15:03
Looking at your example I see that you are using an enum as the `type` parameter -- the purpose of `type` is to provide a mixin data type, such as `int` or `str`, not another enum.

What is your use-case?  Typically, subclassing EnumMeta is not needed.
msg391492 - (view) Author: Suhail S. (suhailsingh247) Date: 2021-04-21 07:00
In my usecase, I wanted to override the behaviour of __getitem__. I was able to accomplish this by subclassing EnumMeta. 

Having done so, I was able to work around this bug as follows. Instead of trying to access the functional API via Enum (using an enum as the type parameter) and failing, I was able to succeed by accessing the functional API via subclass of Enum. Something like:
MyEnum2 = MyEnumBase1(...)
msg391514 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-04-21 12:15
That sounds more like the way it is intended to be used: make you base enum with all the changes you want, then use that base enum either by inheriting from it or as a function call:

    class MyBaseEnum(Enum, metaclass=...):
        ... custom stuff ...
        ... custom stuff ...

    class MyRealEnum1(MyBaseEnum):
        NAME = value
        NAME = value

    MyRealEnum2 = MyBaseEnum("MyRealEnum2", [('NAME', value), ('NAME', value)])

What change did you need with `__getitem__`?  Maybe there is a way to accomplish that goal without subclassing EnumMeta.
msg410828 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-17 22:53
Is there a bug/documentation change to make here or can this be closed?
msg410835 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2022-01-18 00:13
Thanks for the reminder.
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87596
2022-01-18 00:13:43ethan.furmansetstatus: pending -> closed
resolution: not a bug
messages: + msg410835

stage: resolved
2022-01-17 22:53:15iritkatrielsetstatus: open -> pending
versions: + Python 3.10, - Python 3.7, Python 3.8
nosy: + iritkatriel

messages: + msg410828

type: crash -> behavior
2021-04-21 12:15:57ethan.furmansetmessages: + msg391514
2021-04-21 07:00:23suhailsingh247setmessages: + msg391492
2021-04-11 15:03:27ethan.furmansetmessages: + msg390786
2021-03-08 19:02:42kamiltureksetnosy: + kamilturek
2021-03-08 02:02:57ethan.furmansetassignee: ethan.furman
2021-03-08 01:01:52suhailsingh247setnosy: + barry, eli.bendersky, ethan.furman
2021-03-08 00:58:27suhailsingh247create