Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TYP: semantics of enums in stub files changed #29362

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
timhoffm merged 4 commits into matplotlib:main from tacaswell:fix/mypy_enum_semantics
Dec 21, 2024

Conversation

Copy link
Member

@tacaswell tacaswell commented Dec 20, 2024

See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members for details

I think this fix the mypy failures that have started happening on all PRs.

Copy link
Member Author

The issue is that we define an Enum subclass that has no members (because we subsequently sub-class it to add the actual values), I have a 🔨 solution.

Comment on lines 72 to 77
def _generate_next_value_(name, start, count, last_values):
return name

def __hash__(self):
return str(self).__hash__()

Copy link
Contributor

@greglucas greglucas Dec 20, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need these in all of the classes?

  • Can we just explicitly list the string enumeration rather than auto? miter = "miter" (a bit odd we didn't go with caps for the enum JointStyle.MITER is typically how I see them written...
  • Does __hash__ not get string's hash automatically from subclassing?

edit: also a bummer we can't just directly go to StrEnum which looks like it was only released in 3.11: https://docs.python.org/3/library/enum.html#enum.StrEnum

Copy link
Member Author

@tacaswell tacaswell Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"all" is only two, but this also seems to have also not worked (other attempts failed on import...this imported but made the tests fail).

greglucas reacted with thumbs up emoji
Copy link
Member Author

Sigh, I thought it was working locally because I was using an old mypy 😞

Copy link
Member Author

ok, 5th times the charm!

@greglucas noted that __hash__ was doing nothing (and moving it to the subclasses is what broke the tests) so the final version is to move just the _generate_next_value_ function to both sub-classes. At this point I think a single line function duplicated 2x is simpler than doing inheritance from a private class (that we want to get rid of anyway).

On the other hand, doing a conditional import on StrEnum may be better?

tacaswell and others added 4 commits December 21, 2024 16:32
- The definition of `__hash__` was doing nothing
- having a copy of a 1 line function in two sub-classes is simpler than having
 doing inheritance
- fixes an issues with mypy failing on an Enum class with no entries
Co-authored-by: Greg Lucas <greg.m.lucas@gmail.com>
Copy link
Member

I've taken the liberty to add the missing space and force-push to keep the sequence of commits. The way they are written, I assume they should not be squashed.

@timhoffm timhoffm merged commit 677d990 into matplotlib:main Dec 21, 2024
39 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 21, 2024
timhoffm added a commit that referenced this pull request Dec 21, 2024
...362-on-v3.10.x
Backport PR #29362 on branch v3.10.x (TYP: semantics of enums in stub files changed)
Copy link
Member

QuLogic commented Jan 10, 2025

@meeseeksdev backport to v3.10.0-doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@QuLogic QuLogic QuLogic left review comments

@timhoffm timhoffm timhoffm approved these changes

@greglucas greglucas greglucas approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.10.1
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /