diff --git a/Lib/enum.py b/Lib/enum.py index c00ae85d2f8efe..d88133a6a7aac3 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -773,22 +773,31 @@ def __delattr__(cls, attr): super().__delattr__(attr) def __dir__(cls): - interesting = set([ - '__class__', '__contains__', '__doc__', '__getitem__', - '__iter__', '__len__', '__members__', '__module__', - '__name__', '__qualname__', - ] - + cls._member_names_ - ) - if cls._new_member_ is not object.__new__: - interesting.add('__new__') - if cls.__init_subclass__ is not object.__init_subclass__: - interesting.add('__init_subclass__') - if cls._member_type_ is object: - return sorted(interesting) - else: - # return whatever mixed-in data type has - return sorted(set(dir(cls._member_type_)) | interesting) + interesting = set([ + '__class__', '__contains__', '__doc__', '__getitem__', + '__iter__', '__len__', '__members__', '__module__', + '__name__', '__qualname__', + ] + cls._member_names_) + + if cls._new_member_ is not object.__new__: + interesting.add('__new__') + if cls.__init_subclass__ is not object.__init_subclass__: + interesting.add('__init_subclass__') + + # SGP: Include documented public _sunder_ helpers defined on the Enum class. + # SGP: This makes them discoverable via dir(Enum) so rlcompleter can surface + # SGP: them in REPL completions. (rlcompleter drives dotted-name completion + # SGP: from dir(); _add_alias_/_add_value_alias_ are supported _sunder_ APIs.) + for _n in ("_add_alias_", "_add_value_alias_"): + if _n in cls.__dict__: + interesting.add(_n) + + if cls._member_type_ is object: + return sorted(interesting) + else: + # return whatever mixed-in data type has + # SGP: union the mixin's dir() with 'interesting' + return sorted(set(dir(cls._member_type_)) | interesting) def __getitem__(cls, name): """ diff --git a/Misc/NEWS.d/next/Library/2025-09-29-14-39-34.gh-issue-139398.tbFBKV.rst b/Misc/NEWS.d/next/Library/2025-09-29-14-39-34.gh-issue-139398.tbFBKV.rst new file mode 100644 index 00000000000000..476f765b1d9179 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-29-14-39-34.gh-issue-139398.tbFBKV.rst @@ -0,0 +1 @@ +dir(Enum) now includes public _sunder_ helpers (_add_alias_, _add_value_alias_) for better REPL completion. (gh-139398)