Skip to content

[mypyc] Check identity in membership tests against tuple/list displays - #22328

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-971-in-identity
Oct 30, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-971-in-identity

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

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:

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.

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.
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@JukkaL
JukkaL merged commit 3d3488a into master Oct 30, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-971-in-identity branch October 30, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimized membership tests against constant tuples and lists don't include identity checks as per the language reference

2 participants