Skip to content

Better support for SomeEnum.item == some_literal #19594

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Aug 4, 2025

Fixes #19576.
Fixes #16327.
Fixes #17162.

This needs more feedback: is the logic I propose reasonable? Do we want to be stricter and reject such comparisons altogether? Or maybe only reject them if values are incompatible?

This comment has been minimized.

Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

I was a bit confused about this in the original issue too

a = "b"
b = "a"

A.a == "a"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not clear to me why A.a == "a" should be allowed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, that's basically what I want to gather feedback on: at runtime it's true, so there is some merit in allowing such comparisons (see also #17162 for exactly such example).

I'll need to update all added tests to check reachability - if the comparison is not considered non-overlapping, corresponding if bodies must be reachable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did I misunderstand something?

>>> import enum
>>> class X(enum.Enum):
...   a = "b"
... 
>>> X.a == "a"
False

Copy link
Collaborator Author

@sterliakov sterliakov Aug 5, 2025

Choose a reason for hiding this comment

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

Plain enum.Enum doesn't compare equal, of course.

In the test case, however, I'm checking StrEnum and str, Enum subclasses, and they do compare equal to strings matching the values:

import enum

class A(str, enum.Enum):
    A = 'a'

A.A == 'a'  # True

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(so in #17162 both cases must be reachable, and IMO shouldn't show comparison-overlap errors)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we might be talking about different things. I'm referring to an X.a which is a "b", not a "a". I somehow missed that it doesn't return True for non-string-enums though. For reference:

>>> import enum
>>> class X(str, enum.Enum):
...   a = "b"
... 
>>> X.a == "a"  # this is currently allowed, but returns False
False
>>> X.a == "b"  # this is currently disallowed, but returns True
True

While this PR handles the second case, I think the first case matters too. (Though I haven't cared enough to look at mypy internals and see why it happens ATM)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ough, sorry, I must return to this tomorrow with more focus. Yep, that's the question I asked in the PR description: should we reject such comparisons with an incompatible by value literal? I don't have any strong preference here, except that this implementation complexity may not be worth the benefits: how often do people compare two literals for equality? I don't see any obvious use case for that, so just saying "ok, this looks good enough, such comparison is fine at type level" might be a better strategy. Implementing value-based checks would be slightly less trivial.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO, being more smart is good, especially since I doubt it's that many extra lines?

I agree many people may not directly compare literals by value, but I imagine it's useful for like type narrowing. (since we treat enums as a union of their literal elements, I think?)

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Aug 8, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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