Skip to content

Commit 0cc08c6

Browse files
RafaelWOSimonGPrs
andcommitted
Add supported sunder names to Enum __dir__
This change adds the sunder names `_generate_next_value_` and `_missing_` to the `__dir__` method of `EnumType` and `Enum`. In Addition, The instance level sunder names `_add_alias_` and `_add_value_alias_` are added to `Enum.__dir__`. With the sunder names exposed in the `dir()` method, the REPL autocomplete will also show them. Co-Authored-By: SimonGPrs <[email protected]>
1 parent d4e5802 commit 0cc08c6

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

Doc/library/enum.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Data Types
221221
names of the members in *cls*::
222222

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

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

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

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

Lib/enum.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ def __dir__(cls):
777777
'__class__', '__contains__', '__doc__', '__getitem__',
778778
'__iter__', '__len__', '__members__', '__module__',
779779
'__name__', '__qualname__',
780+
# Supported sunder names of Enum class
781+
'_generate_next_value_', '_missing_',
780782
]
781783
+ cls._member_names_
782784
)
@@ -1290,7 +1292,8 @@ def __dir__(self):
12901292
"""
12911293
Returns public methods and other interesting attributes.
12921294
"""
1293-
interesting = set()
1295+
# Initialize with supported sunder names
1296+
interesting = set(('_generate_next_value_', '_missing_', '_add_alias_', '_add_value_alias_'))
12941297
if self.__class__._member_type_ is not object:
12951298
interesting = set(object.__dir__(self))
12961299
for name in getattr(self, '__dict__', []):

Lib/test/test_enum.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5105,6 +5105,8 @@ def test_inspect_getmembers(self):
51055105
('__qualname__', 'TestStdLib.Color'),
51065106
('__init_subclass__', getattr(self.Color, '__init_subclass__')),
51075107
('__iter__', self.Color.__iter__),
5108+
('_missing_', self.Color._missing_),
5109+
('_generate_next_value_', self.Color._generate_next_value_),
51085110
))
51095111
result = dict(inspect.getmembers(self.Color))
51105112
self.assertEqual(set(values.keys()), set(result.keys()))
@@ -5147,6 +5149,10 @@ def test_inspect_classify_class_attrs(self):
51475149
defining_class=self.Color, object='Color'),
51485150
Attribute(name='__qualname__', kind='data',
51495151
defining_class=self.Color, object='TestStdLib.Color'),
5152+
Attribute(name='_missing_', kind='class method',
5153+
defining_class=Enum, object=self.Color._missing_),
5154+
Attribute(name='_generate_next_value_', kind='static method',
5155+
defining_class=self.Color, object=staticmethod(self.Color._generate_next_value_)),
51505156
Attribute(name='YELLOW', kind='data',
51515157
defining_class=self.Color, object=self.Color.YELLOW),
51525158
Attribute(name='MAGENTA', kind='data',
@@ -5533,6 +5539,7 @@ def enum_dir(cls):
55335539
'__class__', '__contains__', '__doc__', '__getitem__',
55345540
'__iter__', '__len__', '__members__', '__module__',
55355541
'__name__', '__qualname__',
5542+
'_generate_next_value_', '_missing_',
55365543
]
55375544
+ cls._member_names_
55385545
)
@@ -5548,7 +5555,8 @@ def enum_dir(cls):
55485555

55495556
def member_dir(member):
55505557
if member.__class__._member_type_ is object:
5551-
allowed = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
5558+
allowed = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value',
5559+
'_generate_next_value_', '_missing_', '_add_alias_', '_add_value_alias_'])
55525560
else:
55535561
allowed = set(dir(member))
55545562
for cls in member.__class__.mro():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add supported \_sunder\_ names to __dir__() method of Enum to support them
2+
in REPL autocompletion.

0 commit comments

Comments
 (0)