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: Enum lookup fails for callable values
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: ethan.furman, typish, xtreak
Priority: normal Keywords:

Created on 2019-10-05 11:56 by typish, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg354003 - (view) Author: Massimo (typish) Date: 2019-10-05 11:56
```
from enum import Enum

class T(Enum):
  TEST = 1

print(T["TEST"])

class S(Enum):
  TEST = lambda a: a

print(S["TEST"])
```
fails with `KeyError: 'TEST'` on the last line.
msg354008 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-05 13:36
https://docs.python.org/3/library/enum.html#allowed-members-and-attributes-of-enumerations

> The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (__str__(), __add__(), etc.), descriptors (methods are also descriptors), and variable names listed in _ignore_.

Looking at the code _member_names has the list of member names internally that stores the members. I guess with a callable assigned to TEST it becomes a method and is not added as a member as per the above doc to skip descriptor as method.
msg354009 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-05 13:40
Some relevant threads

* https://stackoverflow.com/questions/40338652/how-to-define-enum-values-that-are-functions
* https://mail.python.org/pipermail/python-ideas/2017-April/045428.html
msg354124 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2019-10-07 17:54
This is the intended, and documented, behavior.
msg354127 - (view) Author: Massimo (typish) Date: 2019-10-07 18:59
Where is this documented? Sorry, I couldn't find it.

In any case, I'd say this behaviour is quite unexpected, and unless technical reasons are overwhelming, would be nice to change it.
History
Date User Action Args
2022-04-11 14:59:21adminsetgithub: 82556
2019-10-07 18:59:24typishsetmessages: + msg354127
2019-10-07 17:54:19ethan.furmansetstatus: open -> closed
messages: + msg354124

assignee: ethan.furman
resolution: not a bug
stage: resolved
2019-10-05 13:40:25xtreaksetmessages: + msg354009
2019-10-05 13:36:58xtreaksetnosy: + xtreak
messages: + msg354008
2019-10-05 12:01:00xtreaksetnosy: + ethan.furman
2019-10-05 11:56:36typishcreate