From 1a3a4df39c0b4899b824552fb2ec14dbe7c56730 Mon Sep 17 00:00:00 2001 From: Lokeek Date: Tue, 17 Jun 2025 00:11:13 +0530 Subject: [PATCH] Fix: Include named aliases in dir() for IntFlag instances --- Lib/enum.py | 26 ++++++++++++-------------- Lib/test/test_enum.py | 7 +++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index 01fecca3e5aac0..cc37c8ad64e696 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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): diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 221f9db7763764..dd24dda5ba3355 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -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(): + class MyFlags(IntFlag): + READ = 1 + WRITE = 2 + READ_WRITE = READ | WRITE + + assert 'READ_WRITE' in dir(MyFlags.READ_WRITE) class TestIntFlagFunction(_EnumTests, _MinimalOutputTests, _FlagTests, unittest.TestCase): enum_type = IntFlag