Comparing list Vars with < > <= >= gives wrong results (arrays are compared as strings in JS)
Describe the bug
ArrayVar comparisons (<, >, <=, >=) compile straight to the JS operators, e.g. [10, 20] > [5, 6]. JS turns both arrays into strings ("10,20" vs "5,6") and compares them character by character, so the result often differs from Python's element-wise comparison.
The docs (Var Operations β Comparisons) say "All of the comparison operators are used as expected in python", so I'd expect Python semantics here.
To Reproduce
- Code/Link to Repo:
import reflex as rx
class State(rx.State):
version: list[int] = [1, 10, 0]
def index():
return rx.cond(State.version >= [1, 9, 0], rx.text("new"), rx.text("old"))
This renders "old", while in Python [1, 10, 0] >= [1, 9, 0] is True.
A few more, evaluating the generated JS:
| Expression | Generated JS | JS result | Python |
|---|---|---|---|
rx.Var.create([10, 20]) > [5, 6] |
[10, 20] > [5, 6] |
false |
True |
rx.Var.create([2]) < [10] |
[2] < [10] |
false |
True |
rx.Var.create([1, 9]) >= [1, 10] |
[1, 9] >= [1, 10] |
true |
False |
Expected behavior Element-wise (lexicographic) comparison, matching Python.
Specifics (please complete the following information):
- Python Version: 3.12
- Reflex Version: main @ 656e8c7
- OS: Linux
- Browser (Optional): any (plain JS semantics, also reproduced in node 22)
Additional context
The operations are array_lt/gt/le/ge_operation in packages/reflex-base/src/reflex_base/vars/sequence.py, which emit f"{lhs!s} < {rhs!s}". test_valid_var_operations in tests/units/test_var.py uses [10, 20] vs [5, 6] for these operators, but only checks that no error is raised.
A possible fix is a small pyCompareArrays(a, b) helper in state.js that returns -1/0/1, with the operators emitting pyCompareArrays(lhs, rhs) < 0 and so on, similar to how compareDatetime is used for datetime vars. Happy to open a PR for this.