-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Simplify Enum
definition
#8477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify Enum
definition
#8477
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__format__
and __reduce_ex__
have positional-only arguments on object
, but are positional-or-keyword on Enum
. We should keep the overrides for those methods.
I also think we should keep the __dir__
override. The only reason why we return Iterable[str]
from object.__dir__
is so that the several classes that return tuples from custom __dir__
methods don't get flagged as violating LSP. But we can be more precise here, so why shouldn't we?
There aren't that many classes that define custom |
Valid points! Updated |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Thanks! |
Btw, there are other Probably, it is worth adding. |
I'd be in favour of that! |
Ok, I will do it tomorrow 🙂 |
I removed a bunch of magic methods in
Enum
. Why?__dir__
is not special to be included,object
defines__dir__
as-> Iterable[str]
, which is good enough (no other type intypeshed
defines__dir__
)__hash__
does simply returnint
(at least at 3.11): https://github.com/python/cpython/blob/89f52293281b6efc4ef666ef25e677683821f4b9/Lib/enum.py#L1184-L1185 And again,__hash__(self) -> int
is defined inobject
__format__
is not special. It does not add any new string formatters (at least for 3.11). See https://github.com/python/cpython/blob/89f52293281b6efc4ef666ef25e677683821f4b9/Lib/enum.py#L1181-L1182 Mypy treats classes with custom__format__
as types that provide new string formatting patterns: https://github.com/python/mypy/blob/d2063d260ad3096df78867abf53aa247d67b401e/mypy/checkstrformat.py#L367__reduce_ex__
is defined onobject
and returns the same thing:str | tuple[Any, ...]
, see: https://github.com/python/cpython/blob/89f52293281b6efc4ef666ef25e677683821f4b9/Lib/enum.py#L1187-L1188 and https://github.com/python/cpython/blob/89f52293281b6efc4ef666ef25e677683821f4b9/Lib/enum.py#L1277-L1292