Skip to content
Prev Previous commit
Next Next commit
More tweaks
  • Loading branch information
AlexWaygood committed Oct 30, 2021
commit 6d182a09fa6c3e3fbbdc1930b9684d0c3d6af024
15 changes: 10 additions & 5 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ def __dir__(self):
this_module = globals().values()
is_from_this_module = lambda cls: any(cls is thing for thing in this_module)
first_enum_base = next(cls for cls in mro if is_from_this_module(cls))
ignored = set()
# special-case __new__
ignored = {'__new__'}
add_to_ignored = ignored.add

# We want these added to __dir__
Expand All @@ -661,21 +662,25 @@ def __dir__(self):
cls_lookup = cls.__dict__

# If not an instance of EnumType,
# ensure all attributes excluded from that class's `dir()` are ignored.
# ensure all attributes excluded from that class's `dir()` are ignored here.
if not isinstance(cls, EnumType):
cls_lookup = set(cls_lookup).intersection(dir(cls))

for attr_name in cls_lookup:
# Already seen it? Carry on
if attr_name in cls_dir or attr_name in ignored:
continue
# Exclude all sunders from dir(); __new__ is special-cased
elif attr_name == '__new__' or _is_sunder(attr_name):
continue
# Exclude all sunders from dir()
elif _is_sunder(attr_name):
add_to_ignored(attr_name)
# Not an "enum dunder"? Add it to dir() output.
elif attr_name not in enum_dunders:
add_to_dir(attr_name)
# Is an "enum dunder", and is defined by a class from enum.py? Ignore it.
elif getattr(self, attr_name) is getattr(first_enum_base, attr_name, object()):
add_to_ignored(attr_name)
# Is an "enum dunder", and is either user-defined or defined by a mixin class?
# Add it to dir() output.
else:
add_to_dir(attr_name)

Expand Down