Fix false positive reportUnnecessaryComparison for bytes vs bytearray#11434
Open
behlole wants to merge 3 commits into
Open
Fix false positive reportUnnecessaryComparison for bytes vs bytearray#11434behlole wants to merge 3 commits into
behlole wants to merge 3 commits into
Conversation
isTypeComparable's disjoint-built-in branch already special-cases bool/int because their literal values can collide. Add a parallel case for bytes/bytearray: they are distinct built-in classes, but bytes.__eq__ and bytearray.__eq__ cross-compare by content, so expressions like `b"abc" == bytearray(b"abc")` are True at runtime. The Literal[bytes] form (e.g. `bytearray == b"..."`) is handled by the same branch because the literal is still a bytes instance. Fixes microsoft#11433.
Covers: - bytes <-> bytearray in both directions, with == and != (no diagnostic) - bytearray <-> Literal[bytes] (no diagnostic) - bytes <-> str regression guard (still produces a diagnostic) Refs microsoft#11433.
Runs comparison3.py twice (default rule set, then with reportUnnecessaryComparison = 'error') and asserts diagnostic counts of 0 then 1 — the bytes/bytearray cross-comparisons must NOT fire the diagnostic, while the bytes-vs-str regression guard still must. Refs microsoft#11433.
Author
|
@microsoft-github-policy-service agree |
rchiodo
approved these changes
May 12, 2026
Collaborator
rchiodo
left a comment
There was a problem hiding this comment.
Approved via Review Center.
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 #11433.
Context
isTypeComparableintypeEvaluator.tshas an earlyreturn falsefor the "both operands are disjoint built-in classes" case. That branch already special-casesbool/int(because bool literal values can collide with0/1), butbytesandbytearrayfall through unhandled, even though their__eq__implementations cross-compare by content. This produces a false-positivereportUnnecessaryComparisonfor very common code such as:Change
Add a
bytes/bytearraycomparable case inisTypeComparableparallel to the existingbool/inthandling. If both operands are bytes-like built-ins, treat them as comparable. TheLiteral[bytes]form in the issue is handled by the same code path because aLiteral[bytes]value is still abytesinstance (ClassType.isBuiltIn(t, 'bytes')is true for it).Test plan
New sample
packages/pyright-internal/src/tests/samples/comparison3.pycovers:bytes↔bytearrayin both directions, with==and!=— must NOT emit the diagnostic (validates the new branch).bytearray↔Literal[bytes]— must NOT emit (validates the literal form from the issue).bytes↔str— must still emit (regression guard: the new branch must not over-relax).Comparison3test intypeEvaluator6.test.tsruns the sample twice (default rule set off, then withreportUnnecessaryComparison = 'error') and asserts the expected diagnostic count (0, then 1).Notes
Considered also adding
memoryviewto the bytes-like set, sincememoryview(b"a") == b"a"is alsoTrueat runtime, but kept it out of this PR to match the issue's scope exactly. Happy to add it in a follow-up if maintainers prefer.