Repository navigation
[mypyc] Check identity in membership tests against tuple/list displays - #22328
Merged
Merged
Conversation
Fixes mypyc/mypyc#971. Membership tests such as `x in (a, b)` or `x in [a, b]` were compiled to `x == a or x == b`, but Python first checks identity (`x is a or x == a or ...`), so the result could be different for objects that aren't equal to themselves: ```python class C: def __eq__(self, other: object) -> bool: return False c = C() print(c in (c,)) # False when compiled, True when interpreted ``` Also `x not in (a, b)` was compiled to `x != a and x != b`, which calls `__ne__` instead of `__eq__`. Now it's compiled as `not (x in (a, b))`. The identity check is omitted for unboxed values, which have no object identity, and for strings, since string equality checks already check identity. This means that `x in (y,)` is False when compiled if `x` and `y` are both float NaN values, even if they refer to the same object in interpreted Python. This is fine, since floats are value types in mypyc, unlike in Python.
p-sawicki
approved these changes
Oct 30, 2026
Comment on lines
+1282
to
+1286
| if item is left: | ||
| # Same value (e.g. "x in (x,)"), so it's always found | ||
| builder.goto(found) | ||
| builder.activate_block(BasicBlock()) | ||
| break |
Collaborator
There was a problem hiding this comment.
could we check this before the loop so expressions like x in (a, b, c, x) go to found immediately? i think now we would still first compare with all preceding elements.
Collaborator
Author
There was a problem hiding this comment.
I think comparing the preceding items matches CPython semantics (equality comparison can run arbitrary code), so I'd keep it as is. This case isn't useful really, so performance improvement is theoretical.
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 mypyc/mypyc#971.
Membership tests such as
x in (a, b)orx in [a, b]were compiled tox == a or x == b, but Python first checks identity (x is a or x == a or ...), so the result could be different for objects that aren't equal to themselves:Also
x not in (a, b)was compiled tox != a and x != b, which calls__ne__instead of__eq__. Now it's compiled asnot (x in (a, b)).The identity check is omitted for unboxed values, which have no object identity, and for strings, since string equality checks already check identity. This means that
x in (y,)is False when compiled ifxandyare both float NaN values, even if they refer to the same object in interpreted Python. This is fine, since floats are value types in mypyc, unlike in Python.