Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle python range passed to rx.Var.create #4716

Merged
merged 2 commits into from
Jan 31, 2025
Merged
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
12 changes: 10 additions & 2 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,12 @@ class ToVarOperation(ToOperation, cls):

_default_var_type: ClassVar[GenericType] = default_type

ToVarOperation.__name__ = f'To{cls.__name__.removesuffix("Var")}Operation'
new_to_var_operation_name = f"To{cls.__name__.removesuffix('Var')}Operation"
ToVarOperation.__qualname__ = (
ToVarOperation.__qualname__.removesuffix(ToVarOperation.__name__)
+ new_to_var_operation_name
)
ToVarOperation.__name__ = new_to_var_operation_name

_var_subclasses.append(VarSubclassEntry(cls, ToVarOperation, python_types))

Expand Down Expand Up @@ -1385,7 +1390,7 @@ def create( # pyright: ignore [reportArgumentType]
TypeError: If the value is not a supported type for LiteralVar.
"""
from .object import LiteralObjectVar
from .sequence import LiteralStringVar
from .sequence import ArrayVar, LiteralStringVar

if isinstance(value, Var):
if _var_data is None:
Expand Down Expand Up @@ -1441,6 +1446,9 @@ def create( # pyright: ignore [reportArgumentType]
_var_data=_var_data,
)

if isinstance(value, range):
return ArrayVar.range(value.start, value.stop, value.step)

raise TypeError(
f"Unsupported type {type(value)} for LiteralVar. Tried to create a LiteralVar from {value}."
)
Expand Down
2 changes: 1 addition & 1 deletion reflex/vars/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ def array_range_operation(
The range of numbers.
"""
return var_operation_return(
js_expression=f"Array.from({{ length: ({stop!s} - {start!s}) / {step!s} }}, (_, i) => {start!s} + i * {step!s})",
js_expression=f"Array.from({{ length: Math.ceil(({stop!s} - {start!s}) / {step!s}) }}, (_, i) => {start!s} + i * {step!s})",
var_type=List[int],
)

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_var_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ def index():
),
id="foreach_in_match",
),
# Literal range var in a foreach
rx.box(rx.foreach(range(42, 80, 27), rx.text.span), id="range_in_foreach1"),
rx.box(rx.foreach(range(42, 80, 3), rx.text.span), id="range_in_foreach2"),
rx.box(rx.foreach(range(42, 20, -6), rx.text.span), id="range_in_foreach3"),
rx.box(rx.foreach(range(42, 43, 5), rx.text.span), id="range_in_foreach4"),
)


Expand Down Expand Up @@ -799,6 +804,11 @@ def test_var_operations(driver, var_operations: AppHarness):
("memo_comp_nested", "345"),
# foreach in a match
("foreach_in_match", "first\nsecond\nthird"),
# literal range in a foreach
("range_in_foreach1", "4269"),
("range_in_foreach2", "42454851545760636669727578"),
("range_in_foreach3", "42363024"),
("range_in_foreach4", "42"),
]

for tag, expected in tests:
Expand Down
8 changes: 4 additions & 4 deletions tests/units/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,19 +1076,19 @@ def test_array_operations():
assert str(array_var.reverse()) == "[1, 2, 3, 4, 5].slice().reverse()"
assert (
str(ArrayVar.range(10))
== "Array.from({ length: (10 - 0) / 1 }, (_, i) => 0 + i * 1)"
== "Array.from({ length: Math.ceil((10 - 0) / 1) }, (_, i) => 0 + i * 1)"
)
assert (
str(ArrayVar.range(1, 10))
== "Array.from({ length: (10 - 1) / 1 }, (_, i) => 1 + i * 1)"
== "Array.from({ length: Math.ceil((10 - 1) / 1) }, (_, i) => 1 + i * 1)"
)
assert (
str(ArrayVar.range(1, 10, 2))
== "Array.from({ length: (10 - 1) / 2 }, (_, i) => 1 + i * 2)"
== "Array.from({ length: Math.ceil((10 - 1) / 2) }, (_, i) => 1 + i * 2)"
)
assert (
str(ArrayVar.range(1, 10, -1))
== "Array.from({ length: (10 - 1) / -1 }, (_, i) => 1 + i * -1)"
== "Array.from({ length: Math.ceil((10 - 1) / -1) }, (_, i) => 1 + i * -1)"
)


Expand Down