From 4a9c906427ab88900e3600d6a476a9fdbe7d07ef Mon Sep 17 00:00:00 2001 From: Piotr Sawicki Date: Thu, 3 Jul 2025 22:03:51 +0200 Subject: [PATCH 1/2] Fix comparison of tuples with different lengths --- mypyc/irbuild/ll_builder.py | 4 ++++ mypyc/test-data/run-tuples.test | 6 ++++++ 2 files changed, 10 insertions(+) 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..6f634373be3b 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -203,6 +203,12 @@ 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)) + # Test that order is irrelevant to unions. Really I only care that this builds. class A: From 12f517c5dbbdd71e4fb2e3805d0f37f71b824db8 Mon Sep 17 00:00:00 2001 From: Piotr Sawicki Date: Fri, 4 Jul 2025 11:37:25 +0200 Subject: [PATCH 2/2] Add more test cases --- mypyc/test-data/run-tuples.test | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index 6f634373be3b..fe9a8dff08c6 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -206,9 +206,19 @@ def test_unbox_tuple() -> None: 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: