Skip to content
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

Enums: Teach mypy that descriptors are not converted to become enum members #12669

Closed
wants to merge 15 commits into from

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Apr 25, 2022

Fixes #12494.

Description

At runtime, most objects in an enum class statement are automatically converted by the enum module into members of the enumeration. However, if an object has a __get__, __set__ or __delete__ method, special-casing by the enum module means that the object is not converted into an enum member. Mypy is currently aware of this special-casing, leading it to falsely assume that certain enum classes are unsubclassable, when, in fact, they are.

This PR teaches mypy about descriptor special-casing in enums.

Test Plan

  • Two test cases have been added.
  • One test case has been extended.
  • One test case has been altered slightly -- it was in fact buggy. (It shouldn't have been passing.)

@github-actions

This comment has been minimized.

@AlexWaygood

This comment was marked as outdated.

@AlexWaygood AlexWaygood marked this pull request as draft April 26, 2022 01:33
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as ready for review April 26, 2022 18:38
@AlexWaygood AlexWaygood changed the title Make enums with only descriptor members subclassable Enums: Teach mypy that descriptors are not converted to become enum members Apr 26, 2022
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as draft May 15, 2022 11:08
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

mypy/checker.py Outdated Show resolved Hide resolved
mypy/plugins/enums.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

@AlexWaygood
Copy link
Member Author

I doubt I'll get to this any time soon

@finite-state-machine
Copy link

For what it's worth: this issue is very difficult to work around using type: ignore or cast(), especially if the Enum subclass is in a library: you'd have to type: ignore every place where the descriptor was referenced.

Any suggestions would be most welcome.

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.

Enums with only descriptor members should not be final
2 participants