diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2022-01-18 23:13:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-18 23:13:13 (GMT) |
commit | 7c0914d35eaaab2f323260ba5fe8884732533888 (patch) | |
tree | c5d3767758a95f8c2e00ad27f0f276bb7445f4b9 /Lib/enum.py | |
parent | 3852269b91fcc8ee668cd876b3669eba6da5b1ac (diff) | |
download | cpython-7c0914d35eaaab2f323260ba5fe8884732533888.zip cpython-7c0914d35eaaab2f323260ba5fe8884732533888.tar.gz cpython-7c0914d35eaaab2f323260ba5fe8884732533888.tar.bz2 |
bpo-45535: [Enum] include special dunders in dir() (GH-30677)
Include the `__dunders__` in `dir()` that make `Enum` special:
- `__contains__`
- `__getitem__`
- `__iter__`
- `__len__`
- `__members__`
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 772e1ea..b510467 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -766,29 +766,22 @@ class EnumType(type): super().__delattr__(attr) def __dir__(cls): - # TODO: check for custom __init__, __new__, __format__, __repr__, __str__, __init_subclass__ - # on object-based enums + 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: - interesting = set(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__') - for method in ('__init__', '__format__', '__repr__', '__str__'): - if getattr(cls, method) not in (getattr(Enum, method), getattr(Flag, method)): - interesting.add(method) - return sorted(set([ - '__class__', '__contains__', '__doc__', '__getitem__', - '__iter__', '__len__', '__members__', '__module__', - '__name__', '__qualname__', - ]) | interesting - ) + return sorted(interesting) else: # return whatever mixed-in data type has - return sorted(set( - dir(cls._member_type_) - + cls._member_names_ - )) + return sorted(set(dir(cls._member_type_)) | interesting) def __getattr__(cls, name): """ |