Skip to content

Commit a526094

Browse files
authored
Update enum.py [enum] Make dir(Enum) include public _sunder_ helpers for REPL completions (gh-139398)
- I guess this enhances EnumType.__dir__ to include documented public _sunder_ helpers (_add_alias_, _add_value_alias_) if present on the Enum class - I think this could enabrle rlcompleter and the REPL show completions for these supported helpers and matching the intent of the enum API docs... - Normally I think no change to other (private or internal) _sunder_ names - Fixes: gh-139398
1 parent 9c6a1f8 commit a526094

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

Lib/enum.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -773,22 +773,31 @@ def __delattr__(cls, attr):
773773
super().__delattr__(attr)
774774

775775
def __dir__(cls):
776-
interesting = set([
777-
'__class__', '__contains__', '__doc__', '__getitem__',
778-
'__iter__', '__len__', '__members__', '__module__',
779-
'__name__', '__qualname__',
780-
]
781-
+ cls._member_names_
782-
)
783-
if cls._new_member_ is not object.__new__:
784-
interesting.add('__new__')
785-
if cls.__init_subclass__ is not object.__init_subclass__:
786-
interesting.add('__init_subclass__')
787-
if cls._member_type_ is object:
788-
return sorted(interesting)
789-
else:
790-
# return whatever mixed-in data type has
791-
return sorted(set(dir(cls._member_type_)) | interesting)
776+
interesting = set([
777+
'__class__', '__contains__', '__doc__', '__getitem__',
778+
'__iter__', '__len__', '__members__', '__module__',
779+
'__name__', '__qualname__',
780+
] + cls._member_names_)
781+
782+
if cls._new_member_ is not object.__new__:
783+
interesting.add('__new__')
784+
if cls.__init_subclass__ is not object.__init_subclass__:
785+
interesting.add('__init_subclass__')
786+
787+
# SGP: Include documented public _sunder_ helpers defined on the Enum class.
788+
# SGP: This makes them discoverable via dir(Enum) so rlcompleter can surface
789+
# SGP: them in REPL completions. (rlcompleter drives dotted-name completion
790+
# SGP: from dir(); _add_alias_/_add_value_alias_ are supported _sunder_ APIs.)
791+
for _n in ("_add_alias_", "_add_value_alias_"):
792+
if _n in cls.__dict__:
793+
interesting.add(_n)
794+
795+
if cls._member_type_ is object:
796+
return sorted(interesting)
797+
else:
798+
# return whatever mixed-in data type has
799+
# SGP: union the mixin's dir() with 'interesting'
800+
return sorted(set(dir(cls._member_type_)) | interesting)
792801

793802
def __getitem__(cls, name):
794803
"""

0 commit comments

Comments
 (0)