Fix false positive comparison-overlap for str/int enum members#21519
Open
LeSingh1 wants to merge 1 commit into
Open
Fix false positive comparison-overlap for str/int enum members#21519LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
Mypy tracks enum literals by member name rather than runtime value, so `A.a == "b"` (where `A.a.value == "b"`) was flagged as a non-overlapping equality check under --strict-equality. Mixin enums like `class A(str, Enum)` and IntEnum compare equal to plain values of the mixin type at runtime, so this is a false positive. Treat the comparison as potentially overlapping when one side is an enum literal whose enum class mixes in a base type that the other side is also a value of. Fixes python#19576.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: optuna (https://github.com/optuna/optuna)
+ tests/storages_tests/test_heartbeat.py:74: error: Unused "type: ignore" comment [unused-ignore]
+ tests/storages_tests/test_heartbeat.py:245: error: Unused "type: ignore" comment [unused-ignore]
psycopg (https://github.com/psycopg/psycopg)
+ tests/test_pipeline_async.py:57: error: Unused "type: ignore" comment [unused-ignore]
+ tests/test_pipeline.py:60: error: Unused "type: ignore" comment [unused-ignore]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19576.
Mypy represents an enum literal by its member name (a string) regardless of the member's actual value, so under
--strict-equalityit currently flagseven though the comparison evaluates to
Trueat runtime.A.ahas literal typeLiteral[A.a]with literal value"a"(the member name), sois_overlapping_typesdoesn't see any overlap withLiteral['b']. The same false positive applies toenum.StrEnumand toIntEnummembers compared againstintliterals.This change treats the comparison as potentially overlapping when one side is an enum literal whose enum class mixes in a non-enum base (e.g.
str,int) that the other side is also a value of. The existing non-overlap checks for plainEnum(no mixin) and for mismatched mixin types are preserved, soclass B(Enum): x = 1; B.x == 1andMyStrEnum.member == 1still error as before.A regression test
testStrEnumEqualityMemberNameDiffersFromValueis added tocheck-enum.testcoveringclass A(str, Enum),enum.StrEnum,IntEnum, and the plain-Enumnon-regression case.