Skip to content

Navigation Menu

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

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
merged 4 commits into from
Dec 21, 2024

Conversation

tacaswell
Copy link
Member

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.

@tacaswell
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

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

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).

@tacaswell
Copy link
Member Author

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

@tacaswell
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?

lib/matplotlib/_enums.py Outdated Show resolved Hide resolved
lib/matplotlib/_enums.py Outdated Show resolved Hide resolved
lib/matplotlib/backends/registry.pyi Outdated Show resolved Hide resolved
lib/matplotlib/_enums.py Outdated Show resolved Hide resolved
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>
@timhoffm timhoffm force-pushed the fix/mypy_enum_semantics branch from bbe91c9 to 91b26c3 Compare December 21, 2024 15:33
@timhoffm
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)
@QuLogic
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.