Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compile the `%` operator on number Vars with Python semantics, so the result takes the sign of the divisor (`-7 % 3` is now `2` on the frontend, not `-1`).
6 changes: 5 additions & 1 deletion packages/reflex-base/src/reflex_base/vars/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ def number_modulo_operation(lhs: NumberVar, rhs: NumberVar):
Returns:
The number modulo operation.
"""
return f"({lhs!s} % {rhs!s})"
# JavaScript's `%` takes the sign of the dividend (`-7 % 3` is -1) where
# Python's takes the sign of the divisor (`-7 % 3` is 2), so a nonzero
# remainder whose sign differs from the divisor's is shifted by the divisor
# and a zero remainder takes the divisor's sign (`6.0 % -3.0` is -0.0).
return f"((a, b) => {{ const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; }})({lhs!s}, {rhs!s})"


_STARTS_WITH_UNARY_OPERATOR = re.compile(r"[-+!~]|(?:typeof|void|delete|await)\b")
Expand Down
9 changes: 9 additions & 0 deletions tests/units/reflex_base/vars/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ def test_exponent_parenthesizes_a_unary_base():
assert str(-(x**2)) == "-((x ** 2))"
assert str(x**-1) == "(x ** -1)"
assert str(2**-x) == "(2 ** -(x))"


def test_modulo_takes_the_sign_of_the_divisor():
"""Python's `%` takes the sign of the divisor (`-7 % 3` is 2), JavaScript's the dividend's."""
x = Var(_js_expr="x").to(int)
py_mod = "((a, b) => { const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; })"
assert str(LiteralNumberVar.create(-7) % 3) == f"{py_mod}(-7, 3)"
assert str(x % -2.5) == f"{py_mod}(x, -2.5)"
assert str(5 % x) == f"{py_mod}(5, x)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: The PR description says the modulo tests cover zero and infinite operands, but this test has neither. Add assertions for a zero-remainder case such as -6 % 3 and an infinite divisor such as x % float("inf"), or correct the description.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/units/reflex_base/vars/test_number.py, line 24:

<comment>The PR description says the modulo tests cover zero and infinite operands, but this test has neither. Add assertions for a zero-remainder case such as `-6 % 3` and an infinite divisor such as `x % float("inf")`, or correct the description.</comment>

<file context>
@@ -13,3 +13,12 @@ def test_exponent_parenthesizes_a_unary_base():
+    py_mod = "((a, b) => { const r = a % b; return r !== 0 && (r < 0) !== (b < 0) ? r + b : r; })"
+    assert str(LiteralNumberVar.create(-7) % 3) == f"{py_mod}(-7, 3)"
+    assert str(x % -2.5) == f"{py_mod}(x, -2.5)"
+    assert str(5 % x) == f"{py_mod}(5, x)"
</file context>
Suggested change
assert str(5 % x) == f"{py_mod}(5, x)"
assert str(5 % x) == f"{py_mod}(5, x)"
assert str(LiteralNumberVar.create(-6) % 3) == f"{py_mod}(-6, 3)"

11 changes: 7 additions & 4 deletions tests/units/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ def test_basic_operations(TestObj):
assert str(LiteralNumberVar.create(1) * 2) == "(1 * 2)"
assert str(LiteralNumberVar.create(1) / 2) == "(1 / 2)"
assert str(LiteralNumberVar.create(1) // 2) == "Math.floor(1 / 2)"
assert str(LiteralNumberVar.create(1) % 2) == "(1 % 2)"
assert (
str(LiteralNumberVar.create(1) % 2)
== "((a, b) => { const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; })(1, 2)"
)
assert str(LiteralNumberVar.create(1) ** 2) == "(1 ** 2)"
assert str(LiteralNumberVar.create(1) & v(2)) == "pyAnd(1, () => (2))"
assert str(LiteralNumberVar.create(1) | v(2)) == "pyOr(1, () => (2))"
Expand Down Expand Up @@ -1172,7 +1175,7 @@ def test_all_number_operations():

assert (
str(complicated_number)
== "((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2)"
== "(((a, b) => { const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; })(Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2), 3) ** 2)"
)

even_more_complicated_number = ~(
Expand All @@ -1181,7 +1184,7 @@ def test_all_number_operations():

assert (
str(even_more_complicated_number)
== "!(isTrue(pyOr(Math.abs(Math.floor(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))), () => (pyAnd(2, () => (Math.round(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))))))))"
== "!(isTrue(pyOr(Math.abs(Math.floor((((a, b) => { const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; })(Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2), 3) ** 2))), () => (pyAnd(2, () => (Math.round((((a, b) => { const r = a % b; return r === 0 ? (b < 0 ? -0 : 0) : (r < 0) !== (b < 0) ? r + b : r; })(Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2), 3) ** 2))))))))"
)

assert str(LiteralNumberVar.create(5) > False) == "(5 > 0)"
Expand Down Expand Up @@ -1410,7 +1413,7 @@ def test_array_filter():
# non-boolean results are evaluated with python truthiness
truthy_predicate = array_var.filter(lambda x: x % 2)
assert re.fullmatch(
r"\[1, 2, 3\]\.filter\(\(\((\w+)\) => isTrue\(\(\1 % 2\)\)\)\)",
r"\[1, 2, 3\]\.filter\(\(\((\w+)\) => isTrue\(\(\(a, b\) => \{ const r = a % b; return r === 0 \? \(b < 0 \? -0 : 0\) : \(r < 0\) !== \(b < 0\) \? r \+ b : r; \}\)\(\1, 2\)\)\)\)",
str(truthy_predicate),
)
_assert_var_imports(truthy_predicate, "isTrue")
Expand Down