Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-135559: Include named aliases in dir() for IntFlag instances #135584

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 12 additions & 14 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,31 +1265,29 @@ def __str__(self):

def __dir__(self):
"""
Returns public methods and other interesting attributes.
Returns public methods and all named enum members.
"""
interesting = set()
if self.__class__._member_type_ is not object:
interesting = set(object.__dir__(self))

for name in getattr(self, '__dict__', []):
if name[0] != '_' and name not in self._member_map_:
interesting.add(name)

interesting.update(
name for name, member in self.__class__.__members__.items()
if member._name_ is not None
)

for cls in self.__class__.mro():
for name, obj in cls.__dict__.items():
if name[0] == '_':
continue
if isinstance(obj, property):
# that's an enum.property
if obj.fget is not None or name not in self._member_map_:
interesting.add(name)
else:
# in case it was added by `dir(self)`
interesting.discard(name)
elif name not in self._member_map_:
if name[0] != '_' and name not in self._member_map_:
interesting.add(name)

names = sorted(
set(['__class__', '__doc__', '__eq__', '__hash__', '__module__'])
| interesting
)
set(['__class__', '__doc__', '__eq__', '__hash__', '__module__']) | interesting
)
return names

def __format__(self, format_spec):
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,13 @@ def test_shadowed_attr(self):
class TestIntFlagClass(_EnumTests, _MinimalOutputTests, _FlagTests, unittest.TestCase):
enum_type = IntFlag

def test_dir_includes_named_aliases():
Copy link
Member

Choose a reason for hiding this comment

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

This won't work :(
Since we use unittest and not pytest. Please, rewrite this test.

class MyFlags(IntFlag):
READ = 1
WRITE = 2
READ_WRITE = READ | WRITE

assert 'READ_WRITE' in dir(MyFlags.READ_WRITE)
Copy link
Member

Choose a reason for hiding this comment

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

We use self.assert* methods here.
Please, add checks for all related names, not only a single one.

I would propose to add more combitations of values to the test.


class TestIntFlagFunction(_EnumTests, _MinimalOutputTests, _FlagTests, unittest.TestCase):
enum_type = IntFlag
Expand Down
Loading