Fix false positive reportUnnecessaryComparison for bytes-like types - #11547
Open
pctablet505 wants to merge 1 commit into
Open
Fix false positive reportUnnecessaryComparison for bytes-like types#11547pctablet505 wants to merge 1 commit into
pctablet505 wants to merge 1 commit into
Conversation
The reportUnnecessaryComparison check assumed that any two disjoint built-in class instances could never compare equal, which is true for most types but not for "bytes", "bytearray" and "memoryview". These types are otherwise unrelated for assignability purposes, but their "__eq__" implementations do support cross-type content comparisons, so "bytearray(4) == bytes(4)" is valid and can be True. Fixes microsoft#11433
Author
|
@microsoft-github-policy-service agree |
pctablet505
marked this pull request as ready for review
July 17, 2026 09:59
Collaborator
|
🔒 Automated review in progress — @rchiodo is auto-reviewing this PR. |
rchiodo
approved these changes
Jul 27, 2026
rchiodo
left a comment
Collaborator
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
reportUnnecessaryComparisontreats two disjoint built-in class instances as never comparable with==/!=. That's a reasonable default, but it doesn't hold forbytes,bytearrayandmemoryview: they're unrelated for assignability purposes, yet their__eq__implementations do support cross-type content comparisons (bytearray(4) == bytes(4)isTrueat runtime).This adds a narrow carve-out in
isTypeComparablefor this specific group of types, reusing the existingtypePromotionstable that already models the relationship elsewhere (it's the same list consulted for thebytes/bytearray/memoryviewassignability promotion). Comparability isn't gated behinddisableBytesTypePromotions, since that setting is about assignment compatibility, not about whether__eq__can returnTrue.Added test cases to
comparison1.pycoveringbytearray/bytes, abytearray-vs-literal-bytes comparison, and all pairings ofbytes/bytearray/memoryview, alongside a couple of genuinely-disjoint builtin cases (int/str,list/dict) to confirm the general diagnostic still fires where it should.