From 3f11f267b6d70981f9191b07f49c163bddc75cfb Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 14:07:10 -0400 Subject: [PATCH 1/3] [mypyc] feat: support negative index in TupleGet op This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert This won't change functionality on its own, since none of the existing calling locations can pass a negative value, but will allow us to pass negative values in https://github.com/python/mypy/pull/19972 so I think we should consider this PR a prerequisite to that one --- mypyc/ir/ops.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mypyc/ir/ops.py b/mypyc/ir/ops.py index 76c1e07a79d5..2a110b8b11a9 100644 --- a/mypyc/ir/ops.py +++ b/mypyc/ir/ops.py @@ -1045,10 +1045,15 @@ class TupleGet(RegisterOp): def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = False) -> None: super().__init__(line) + assert isinstance( + src.type, RTuple + ), "TupleGet only operates on tuples, not {type(src.type).__name__}" + src_len = len(src.type.types) self.src = src self.index = index - assert isinstance(src.type, RTuple), "TupleGet only operates on tuples" - assert index >= 0 + if index < 0: + self.index += src_len + assert self.index <= src_len - 1, f"Index out of range.\nsource type: {src.type}\nindex: {index}" self.type = src.type.types[index] self.is_borrowed = borrow From 885d785b0a1b00649fba4b170464e76a907d0917 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Oct 2025 18:08:43 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypyc/ir/ops.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypyc/ir/ops.py b/mypyc/ir/ops.py index 2a110b8b11a9..1695267a0d55 100644 --- a/mypyc/ir/ops.py +++ b/mypyc/ir/ops.py @@ -1053,7 +1053,9 @@ def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = Fal self.index = index if index < 0: self.index += src_len - assert self.index <= src_len - 1, f"Index out of range.\nsource type: {src.type}\nindex: {index}" + assert ( + self.index <= src_len - 1 + ), f"Index out of range.\nsource type: {src.type}\nindex: {index}" self.type = src.type.types[index] self.is_borrowed = borrow From 7124fa1424d5715dd8e26932b12742b4bd63161f Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 6 Oct 2025 12:47:00 -0400 Subject: [PATCH 3/3] fix f-string prefix Co-authored-by: Piotr Sawicki --- mypyc/ir/ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/ir/ops.py b/mypyc/ir/ops.py index 1695267a0d55..ffce529f0756 100644 --- a/mypyc/ir/ops.py +++ b/mypyc/ir/ops.py @@ -1047,7 +1047,7 @@ def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = Fal super().__init__(line) assert isinstance( src.type, RTuple - ), "TupleGet only operates on tuples, not {type(src.type).__name__}" + ), f"TupleGet only operates on tuples, not {type(src.type).__name__}" src_len = len(src.type.types) self.src = src self.index = index