Skip to content

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

Merged
merged 2 commits into from
Aug 3, 2022
Merged

Simplify Enum definition #8477

merged 2 commits into from
Aug 3, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Aug 3, 2022

I removed a bunch of magic methods in Enum. Why?

  1. __dir__ is not special to be included, object defines __dir__ as -> Iterable[str], which is good enough (no other type in typeshed defines __dir__)
  2. __hash__ does simply return int (at least at 3.11): https://github.com/python/cpython/blob/89f52293281b6efc4ef666ef25e677683821f4b9/Lib/enum.py#L1184-L1185 And again, __hash__(self) -> int is defined in object
  3. __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
  4. __reduce_ex__ is defined on object 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

@github-actions

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a 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?

@AlexWaygood
Copy link
Member

  1. no other type in typeshed defines __dir__

There aren't that many classes that define custom __dir__ methods :)

@sobolevn
Copy link
Member Author

sobolevn commented Aug 3, 2022

Valid points! Updated

@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood
Copy link
Member

Thanks!

@AlexWaygood AlexWaygood merged commit b19d447 into python:master Aug 3, 2022
@sobolevn
Copy link
Member Author

sobolevn commented Aug 3, 2022

Btw, there are other __dir__ definitions in cpython: https://cs.github.com/python/cpython?q=def+__dir__

Probably, it is worth adding.

@AlexWaygood
Copy link
Member

Btw, there are other __dir__ definitions in cpython: https://cs.github.com/python/cpython?q=def+__dir__

Probably, it is worth adding.

I'd be in favour of that!

@sobolevn
Copy link
Member Author

sobolevn commented Aug 3, 2022

Ok, I will do it tomorrow 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants