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's dir() does not contain inherited members
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder: Enum: modify __repr__, __str__; update docs
View: 40066
Assigned To: ethan.furman Nosy List: AlexWaygood, barry, eli.bendersky, ethan.furman, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-10-20 10:35 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Enum_dir_patch.txt AlexWaygood, 2021-10-21 16:53
enum_dir_output_with_patch.txt AlexWaygood, 2021-10-21 16:54
Pull Requests
URL Status Linked Edit
PR 29316 merged AlexWaygood, 2021-10-29 15:35
PR 30677 merged ethan.furman, 2022-01-18 23:11
Messages (11)
msg404420 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-20 10:35
For example:

>>> from enum import *
>>> class E(IntEnum):
...     x = 1
... 
>>> dir(E)
['__class__', '__doc__', '__members__', '__module__', 'x']
>>> E.from_bytes
<built-in method from_bytes of EnumType object at 0x559d47415ce0>
>>> E.to_bytes
<method 'to_bytes' of 'int' objects>
>>> E.numerator
<attribute 'numerator' of 'int' objects>
>>> E.__add__
<slot wrapper '__add__' of 'int' objects>

There are methods and attributes inherited from int, but they are not shown in dir(). As result they are absent in help() and completion does not work for them.
msg404621 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-10-21 16:53
I had a go at writing a patch for `__dir__` that would include any methods an enum class had acquired through `int`/`str`/etc. mixins, while continuing to ignore enum dunders and sunders (as is currently the case). My patch also allows user-defined methods defined in enum subclasses to show up in the `help()` output, whereas they currently do not, since `help(enum_member)` looks up `dir(enum_class)` rather than `dir(enum_member)`.

The patch is a fair way more complex than the the existing code, however.
msg404634 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-10-21 19:02
Looks interesting, thank you for the patch.
msg404635 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-21 19:33
There may be a simple error (superfluous .__class__), but I am not sure.

BTW, why use mro() instead of __mro__? Most code use __mro__.
msg404721 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-10-22 07:36
Would there be interest in me submitting a PR along these lines?

----

> why use mro() instead of __mro__?

Er, no reason, really. To be honest, I've never really understood why Python has both.
msg405313 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-29 15:44
PR 29316 looks complicated.

First of all, why do Enum needs a custom __dir__? What is wrong with the default implementation?
msg405320 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-10-29 16:55
Enums have had a custom dir() from the beginning, partly because they are not standard objects and do not follow standard rules.

The question posed by this issue is whether Enums with mixed-in data types should show the inherited methods, and if yes, should it also show inherited `__dunders__`.

I'm inclined to say yes for the normal inherited methods, I'm not sure about inherited `__dunders__`.
msg405323 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-10-29 17:06
I would argue it's quite important for `IntEnum` to have the dunder methods inherited from `int` show up in `help()`. Dunder methods indicate that an object has certain behaviours, so it's important for a user to be able to verify that an `IntEnum` member has similar behaviours to an `int` instance. `IntEnum` is advertised in the documentation as something that can be used everywhere where an integer is expected; it's surprising when the output from `help()` is hugely abbreviated for `IntEnum` relative to `int`.

But, I agree that my first draft of this PR is more complex than I'd like it to be.
msg407539 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-12-02 16:50
New changeset b2afdc95cc8f4e9228148730949a43cef0323f15 by Alex Waygood in branch 'main':
bpo-45535: Improve output of Enum ``dir()`` (GH-29316)
https://github.com/python/cpython/commit/b2afdc95cc8f4e9228148730949a43cef0323f15
msg410807 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2022-01-17 17:02
Fixed in 3.11.

Pure enums have a few more dir() entries now; mixed enums now show all inherited methods/attributes -- members still do not show up in member dirs (this is a good thing).
msg410912 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2022-01-18 23:13
New changeset 7c0914d35eaaab2f323260ba5fe8884732533888 by Ethan Furman in branch 'main':
bpo-45535: [Enum] include special dunders in dir() (GH-30677)
https://github.com/python/cpython/commit/7c0914d35eaaab2f323260ba5fe8884732533888
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89698
2022-01-18 23:13:23ethan.furmansetmessages: + msg410912
2022-01-18 23:11:49ethan.furmansetpull_requests: + pull_request28877
2022-01-17 17:02:11ethan.furmansetstatus: open -> closed
superseder: Enum: modify __repr__, __str__; update docs
messages: + msg410807

resolution: fixed
stage: patch review -> resolved
2021-12-02 16:50:06ethan.furmansetmessages: + msg407539
2021-10-29 17:06:55AlexWaygoodsetmessages: + msg405323
2021-10-29 16:55:08ethan.furmansetmessages: + msg405320
2021-10-29 15:44:21serhiy.storchakasetmessages: + msg405313
2021-10-29 15:35:54AlexWaygoodsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27585
2021-10-22 07:36:33AlexWaygoodsetmessages: + msg404721
2021-10-21 19:33:42serhiy.storchakasetmessages: + msg404635
2021-10-21 19:03:18ethan.furmansetassignee: ethan.furman
versions: - Python 3.9
2021-10-21 19:02:36ethan.furmansetmessages: + msg404634
2021-10-21 16:54:24AlexWaygoodsetfiles: + enum_dir_output_with_patch.txt
2021-10-21 16:53:10AlexWaygoodsetfiles: + Enum_dir_patch.txt
nosy: + AlexWaygood
messages: + msg404621

2021-10-20 10:35:06serhiy.storchakacreate