[issue46269] '__new__' is never shown in `dir(SomeEnum)`

Nikita Sobolev report at bugs.python.org
Wed Jan 5 10:15:11 EST 2022


New submission from Nikita Sobolev <mail at sobolevn.me>:

Right now `__new__` is marked to be special-cased in `Enum.__dir__`.
It is generally ignored:
But, (I think that was the original idea), when `__new__` is overridden, it should be added to the output.

But, this line is problematic: https://github.com/python/cpython/blame/46e4c257e7c26c813620232135781e6c53fe8d4d/Lib/enum.py#L656

Why? Because `self.__new__` is always `EnumMeta.__new__`. Original `__new__` is renamed to `_new_member_`.

This behavior is also not tested.

So, my proposal is: let's always remove this method from `__dir__`. Why?
- If we modify the check above to compare `_new_member_`, we will show `__new__` as the output. It is kinda misleading
- `__new__` is not supposed to be overloaded in `EnumMeta`, it is very special
- `__new__` is a very basic thing. It would be kinda strange not to see it for some cases, but to see it for others. For example, all (?) other data types always show `__new__`, even if it is not explicitly defined:

```
>>> class A:
...    def __new__(*args, **kwargs): ...
... 
>>> assert '__new__' in dir(A)

>>> class B: ...
... 
>>> assert '__new__' in dir(B)
```

I guess being consistent here (we don't show `__new__` at all times) is better.

I will send a PR that removes these two lines in a moment! Any other ideas are much appreciated! 

Related:
- https://bugs.python.org/issue45535
- https://github.com/python/cpython/pull/29316

----------
components: Library (Lib)
messages: 409771
nosy: AlexWaygood, ethan.furman, sobolevn
priority: normal
severity: normal
status: open
title: '__new__' is never shown in `dir(SomeEnum)`
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46269>
_______________________________________


More information about the Python-bugs-list mailing list