Skip to content

bpo-40084: Enum.__dir__ listing includes entries from instance dict #19219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def __dir__(self):
for cls in self.__class__.mro()
for m in cls.__dict__
if m[0] != '_' and m not in self._member_map_
]
] + [m for m in self.__dict__ if m[0] != '_']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd prefer if not m.startswith("_") because it's more Pythonic and resilient in case someone smuggles an empty string in.
But, perhaps, m[0] is more performant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance isn't really an issue for a function like dir() (except in extreme cases, of course). In this case we'll stick with m[0] to match what's already being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that m[0] is more performant and I also wanted to keep the consistency with the previous comparison m[0] != '_'

I don't know why it more performant though, I've found the following infos about what is performed:

  • m[0] uses COMPARE_OP which seems to use opcode prediction, paired with POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE
  • startswith implies a call of the builtin function

Thanks for asking, I've learned more on the opcode dispatch while investigating!

return (['__class__', '__doc__', '__module__'] + added_behavior)

def __format__(self, format_spec):
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ class SubEnum(SuperEnum):
set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']),
)

def test_dir_on_sub_with_behavior_including_instance_dict_on_super(self):
# see issue40084
class SuperEnum(IntEnum):
def __new__(cls, value, description=""):
obj = int.__new__(cls, value)
obj._value_ = value
obj.description = description
return obj
class SubEnum(SuperEnum):
sample = 5
self.assertTrue({'description'} <= set(dir(SubEnum.sample)))

def test_enum_in_enum_out(self):
Season = self.Season
self.assertIs(Season(Season.WINTER), Season.WINTER)
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import errno
from http import client
from http import client, HTTPStatus
import io
import itertools
import os
Expand Down Expand Up @@ -493,6 +493,10 @@ def _parse_chunked(self, data):


class BasicTest(TestCase):
def test_dir_with_added_behavior_on_status(self):
# see issue40084
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))

def test_status_lines(self):
# Test HTTP status lines

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Gawain Bolton
Carl Friedrich Bolz-Tereick
Forest Bond
Gregory Bond
Angelin Booz
Médéric Boquien
Matias Bordese
Jonas Borgström
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``Enum.__dir__``: dir(Enum.member) now includes attributes as well as methods.