diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 6bc1eb9d0493..36b7c9241c71 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -1507,6 +1507,10 @@ def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int = -1) -> Val assert isinstance(lhs.type, RTuple) and isinstance(rhs.type, RTuple) equal = True if op == "==" else False result = Register(bool_rprimitive) + # tuples of different lengths + if len(lhs.type.types) != len(rhs.type.types): + self.add(Assign(result, self.false() if equal else self.true(), line)) + return result # empty tuples if len(lhs.type.types) == 0 and len(rhs.type.types) == 0: self.add(Assign(result, self.true() if equal else self.false(), line)) diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index 1437eaef2aa5..fe9a8dff08c6 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -203,6 +203,22 @@ def f7(x: List[Tuple[int, int]]) -> int: def test_unbox_tuple() -> None: assert f7([(5, 6)]) == 11 +def test_comparison() -> None: + assert ('x','y') == ('x','y') + assert not(('x','y') != ('x','y')) + + assert ('x','y') != ('x','y',1) + assert not(('x','y') == ('x','y',1)) + + assert ('x','y',1) != ('x','y') + assert not(('x','y',1) == ('x','y')) + + assert ('x','y') != () + assert not(('x','y') == ()) + + assert () != ('x','y') + assert not(() == ('x','y')) + # Test that order is irrelevant to unions. Really I only care that this builds. class A: