Skip to content

Commit 7ab184a

Browse files
authored
[enum] Make dir(Enum) include public _sunder_ helpers for REPL completions (gh-139398)
Fix indentation in EnumType.__dir__ (gh-139398) Add NEWS entry for enum dir(Enum) completions (gh-139398)
1 parent a526094 commit 7ab184a

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

Lib/enum.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -773,31 +773,37 @@ 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-
] + 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)
776+
interesting = set([
777+
'__class__', '__contains__', '__doc__', '__getitem__',
778+
'__iter__', '__len__', '__members__', '__module__',
779+
'__name__', '__qualname__',
780+
]
781+
+ cls._member_names_
782+
)
783+
784+
if cls._new_member_ is not object.__new__:
785+
interesting.add('__new__')
786+
if cls.__init_subclass__ is not object.__init_subclass__:
787+
interesting.add('__init_subclass__')
788+
789+
# SGP: Include documented public _sunder_ helpers defined on the Enum class.
790+
# SGP: This makes them discoverable via dir(Enum) so rlcompleter can surface
791+
# SGP: them in REPL completions. (rlcompleter drives dotted-name completion
792+
# SGP: from dir(); _add_alias_/_add_value_alias_ are supported _sunder_ APIs.)
793+
for _n in ("_add_alias_", "_add_value_alias_"):
794+
if _n in cls.__dict__:
795+
interesting.add(_n)
796+
797+
if cls._member_type_ is object:
798+
return sorted(interesting)
799+
else:
800+
# return whatever mixed-in data type has
801+
# SGP: union the mixin's dir() with 'interesting'
802+
return sorted(set(dir(cls._member_type_)) | interesting)
803+
804+
805+
806+
801807

802808
def __getitem__(cls, name):
803809
"""

0 commit comments

Comments
 (0)