Skip to content

Commit 4a9c906

Browse files
committed
Fix comparison of tuples with different lengths
1 parent 1bf186c commit 4a9c906

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,10 @@ def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int = -1) -> Val
15071507
assert isinstance(lhs.type, RTuple) and isinstance(rhs.type, RTuple)
15081508
equal = True if op == "==" else False
15091509
result = Register(bool_rprimitive)
1510+
# tuples of different lengths
1511+
if len(lhs.type.types) != len(rhs.type.types):
1512+
self.add(Assign(result, self.false() if equal else self.true(), line))
1513+
return result
15101514
# empty tuples
15111515
if len(lhs.type.types) == 0 and len(rhs.type.types) == 0:
15121516
self.add(Assign(result, self.true() if equal else self.false(), line))

mypyc/test-data/run-tuples.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ def f7(x: List[Tuple[int, int]]) -> int:
203203
def test_unbox_tuple() -> None:
204204
assert f7([(5, 6)]) == 11
205205

206+
def test_comparison() -> None:
207+
assert ('x','y') == ('x','y')
208+
assert not(('x','y') != ('x','y'))
209+
assert ('x','y') != ('x','y',1)
210+
assert not(('x','y') == ('x','y',1))
211+
206212
# Test that order is irrelevant to unions. Really I only care that this builds.
207213

208214
class A:

0 commit comments

Comments
 (0)