Skip to content
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
4 changes: 2 additions & 2 deletions Doc/library/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Data Types
names of the members in *cls*::

>>> dir(Color)
['BLUE', 'GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__', '__len__', '__members__', '__module__', '__name__', '__qualname__']
['BLUE', 'GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__', '__len__', '__members__', '__module__', '__name__', '__qualname__', '_generate_next_value_', '_missing_']

.. method:: EnumType.__getitem__(cls, name)

Expand Down Expand Up @@ -330,7 +330,7 @@ Data Types
... print('today is %s' % cls(date.today().isoweekday()).name)
...
>>> dir(Weekday.SATURDAY)
['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
['__class__', '__doc__', '__eq__', '__hash__', '__module__', '_add_alias_', '_add_value_alias_', '_generate_next_value_', '_missing_', 'name', 'today', 'value']

.. method:: Enum._generate_next_value_(name, start, count, last_values)

Expand Down
5 changes: 4 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ def __dir__(cls):
'__class__', '__contains__', '__doc__', '__getitem__',
'__iter__', '__len__', '__members__', '__module__',
'__name__', '__qualname__',
# Supported sunder names of Enum class
'_generate_next_value_', '_missing_',
]
+ cls._member_names_
)
Expand Down Expand Up @@ -1290,7 +1292,8 @@ def __dir__(self):
"""
Returns public methods and other interesting attributes.
"""
interesting = set()
# Initialize with supported sunder names
interesting = set(('_generate_next_value_', '_missing_', '_add_alias_', '_add_value_alias_'))
if self.__class__._member_type_ is not object:
interesting = set(object.__dir__(self))
for name in getattr(self, '__dict__', []):
Expand Down
14 changes: 11 additions & 3 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5105,6 +5105,8 @@ def test_inspect_getmembers(self):
('__qualname__', 'TestStdLib.Color'),
('__init_subclass__', getattr(self.Color, '__init_subclass__')),
('__iter__', self.Color.__iter__),
('_missing_', self.Color._missing_),
('_generate_next_value_', self.Color._generate_next_value_),
))
result = dict(inspect.getmembers(self.Color))
self.assertEqual(set(values.keys()), set(result.keys()))
Expand Down Expand Up @@ -5147,6 +5149,10 @@ def test_inspect_classify_class_attrs(self):
defining_class=self.Color, object='Color'),
Attribute(name='__qualname__', kind='data',
defining_class=self.Color, object='TestStdLib.Color'),
Attribute(name='_missing_', kind='class method',
defining_class=Enum, object=self.Color._missing_),
Attribute(name='_generate_next_value_', kind='static method',
defining_class=self.Color, object=staticmethod(self.Color._generate_next_value_)),
Comment on lines +5152 to +5155
Copy link
Contributor Author

@RafaelWO RafaelWO Oct 12, 2025

Choose a reason for hiding this comment

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

I tried to adjust this test to expect the new attributes, but I wasn't able to get the test to pass. I guess I need some help here 🙂 thanks!

./python -m unittest -v test.test_enum.TestStdLib.test_inspect_classify_class_attrs

Attribute(name='YELLOW', kind='data',
defining_class=self.Color, object=self.Color.YELLOW),
Attribute(name='MAGENTA', kind='data',
Expand Down Expand Up @@ -5178,10 +5184,10 @@ def test_inspect_classify_class_attrs(self):
# __doc__ is too big to check exactly, so treat the same as __init_subclass__
for name in ('name','kind','defining_class'):
if getattr(v, name) != getattr(r, name):
print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='')
print('\n%s\nexpected: %s\nactual: %s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='')
failed = True
elif r != v:
print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='')
print('\n%s\nexpected: %s\nactual: %s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='')
failed = True
if failed:
self.fail("result does not equal expected, see print above")
Expand Down Expand Up @@ -5533,6 +5539,7 @@ def enum_dir(cls):
'__class__', '__contains__', '__doc__', '__getitem__',
'__iter__', '__len__', '__members__', '__module__',
'__name__', '__qualname__',
'_generate_next_value_', '_missing_',
]
+ cls._member_names_
)
Expand All @@ -5548,7 +5555,8 @@ def enum_dir(cls):

def member_dir(member):
if member.__class__._member_type_ is object:
allowed = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
allowed = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value',
'_generate_next_value_', '_missing_', '_add_alias_', '_add_value_alias_'])
else:
allowed = set(dir(member))
for cls in member.__class__.mro():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add supported ``_sunder_`` names to the :func:`dir` method of the :mod:`enum` module
to support them in :term:`REPL` autocompletion.
Loading