diff --git a/docs/state_structure/overview.md b/docs/state_structure/overview.md index f99034a6d01..75f72ab00b0 100644 --- a/docs/state_structure/overview.md +++ b/docs/state_structure/overview.md @@ -246,6 +246,21 @@ This pattern is useful when you have multiple states that need to interact with If the var is not retrievable, `get_var_value` will raise an `UnretrievableVarValueError`. +The var can also read into a state var by attribute access, item access or indexing. A key that is itself a var is resolved first, and as on the frontend, a missing key or a `None` on the way reads as `None`: + +```python +first_tag = await self.get_var_value(OtherState.user.tags[0]) +selected = await self.get_var_value(OtherState.rows[OtherState.selected_index]) +``` + +`set_var_value` is the counterpart: it sets the value such a var reads. A nested value is set through the state var holding it, which marks that var dirty so the change reaches the frontend. In a background task, call it inside `async with self`, like any other state change. + +```python +await self.set_var_value(OtherState.user.tags[0], "featured") +``` + +A var that cannot be set, such as a computed var or an operation like `OtherState.a + OtherState.b`, raises an `UnsettableVarValueError`. + ## Performance Implications When an event handler is called, Reflex will load the data not only for the substate containing diff --git a/news/+set-var-value.feature.md b/news/+set-var-value.feature.md new file mode 100644 index 00000000000..00f998728b2 --- /dev/null +++ b/news/+set-var-value.feature.md @@ -0,0 +1 @@ +`get_var_value` resolves vars that read into a state var by attribute access, item access or indexing, such as `State.user.tags[0]` or `State.rows[State.index]`, and the new `set_var_value` sets the value such a var reads, marking the state var dirty. diff --git a/packages/reflex-base/news/+var-access-path.feature.md b/packages/reflex-base/news/+var-access-path.feature.md new file mode 100644 index 00000000000..026abc3c9ae --- /dev/null +++ b/packages/reflex-base/news/+var-access-path.feature.md @@ -0,0 +1 @@ +Indexing an array var builds an `ArrayItemOperation` instead of an anonymous var operation, and `reflex_base.vars.object.var_access_path` splits a var such as `State.user.tags[0]` into the var it reads from and the keys it reads on the way. diff --git a/packages/reflex-base/src/reflex_base/utils/exceptions.py b/packages/reflex-base/src/reflex_base/utils/exceptions.py index 75368add0eb..2a6b603a44f 100644 --- a/packages/reflex-base/src/reflex_base/utils/exceptions.py +++ b/packages/reflex-base/src/reflex_base/utils/exceptions.py @@ -314,6 +314,10 @@ class UnretrievableVarValueError(ReflexError): """Raised when the value of a var is not retrievable.""" +class UnsettableVarValueError(ReflexError): + """Raised when the value of a var cannot be set.""" + + class HybridPropertyError(ReflexError): """Raised when a hybrid property is misused while building its frontend var.""" diff --git a/packages/reflex-base/src/reflex_base/vars/object.py b/packages/reflex-base/src/reflex_base/vars/object.py index a223d4131a4..58af8741279 100644 --- a/packages/reflex-base/src/reflex_base/vars/object.py +++ b/packages/reflex-base/src/reflex_base/vars/object.py @@ -30,6 +30,7 @@ from .base import ( CachedVarOperation, LiteralVar, + ToOperation, Var, VarData, cached_property_no_lock, @@ -39,7 +40,7 @@ ) from .hybrid_property import HybridProperty from .number import BooleanVar, NumberVar, raise_unsupported_operand_types -from .sequence import ArrayVar, LiteralArrayVar, StringVar +from .sequence import ArrayItemOperation, ArrayVar, LiteralArrayVar, StringVar OBJECT_TYPE = TypeVar("OBJECT_TYPE", covariant=True) @@ -693,6 +694,38 @@ def create( ) +def var_access_path(var: Var) -> tuple[Var, tuple[Any, ...]]: + """Split a var into the var it reads from and the keys it reads on the way. + + Attribute access, item access and array indexing are followed through any + casts; every other var ends the walk. ``State.user.tags[0]`` splits into + the var of the ``user`` field and ``("tags", 0)``, while a var that reads no + key, such as ``State.a + State.b``, is its own root with no keys. + + Args: + var: The var to split. + + Returns: + The var it reads from, and the keys it reads in order: a literal key as + its Python value, any other key as its var. + """ + keys: list[Var] = [] + while True: + if isinstance(var, ToOperation): + var = var._original + elif isinstance(var, ObjectItemOperation): + keys.append(var._key) + var = var._object + elif isinstance(var, ArrayItemOperation): + keys.append(var._index) + var = var._array + else: + break + return var, tuple( + key._var_value if isinstance(key, LiteralVar) else key for key in reversed(keys) + ) + + @var_operation def object_has_own_property_operation(object: ObjectVar, key: Var): """Check if an object has a key. diff --git a/packages/reflex-base/src/reflex_base/vars/sequence.py b/packages/reflex-base/src/reflex_base/vars/sequence.py index 5c29967d40c..92830bf2946 100644 --- a/packages/reflex-base/src/reflex_base/vars/sequence.py +++ b/packages/reflex-base/src/reflex_base/vars/sequence.py @@ -240,7 +240,7 @@ def __getitem__(self, i: Any) -> ArrayVar[ARRAY_VAR_TYPE] | Var: isinstance(i, NumberVar) and i._is_strict_float() ): raise_unsupported_operand_types("[]", (type(self), type(i))) - return array_item_operation(self, i) + return ArrayItemOperation.create(self, i).guess_type() def length(self) -> NumberVar[int]: """Get the length of the array. @@ -2125,31 +2125,62 @@ def _determine_value_of_array_index( return Any -@var_operation -def array_item_operation(array: ArrayVar, index: NumberVar | int): - """Get an item from an array. - - Args: - array: The array. - index: The index of the item. +@dataclasses.dataclass( + eq=False, + frozen=True, + slots=True, +) +class ArrayItemOperation(CachedVarOperation, Var): + """Operation to get an item from an array.""" - Returns: - The item from the array. - """ - element_type = _determine_value_of_array_index( - array._var_type, - ( - index - if isinstance(index, int) - else (index._var_value if isinstance(index, LiteralNumberVar) else None) - ), + _array: ArrayVar = dataclasses.field( + default_factory=lambda: LiteralArrayVar.create([]) ) - - return var_operation_return( - js_expression=f"{array!s}?.at?.({index!s})", - var_type=element_type, + _index: NumberVar = dataclasses.field( + default_factory=lambda: LiteralNumberVar.create(0) ) + @cached_property_no_lock + def _cached_var_name(self) -> str: + """The name of the operation. + + Returns: + The name of the operation. + """ + return f"{self._array!s}?.at?.({self._index!s})" + + @classmethod + def create( + cls, + array: ArrayVar, + index: NumberVar | int, + _var_data: VarData | None = None, + ) -> ArrayItemOperation: + """Create the array item operation. + + Args: + array: The array to get the item from. + index: The index of the item. + _var_data: Additional hooks and imports associated with the operation. + + Returns: + The array item operation. + """ + # LiteralVar.create renders a bool index as `true`, not Python's `True`. + index_var = index if isinstance(index, Var) else LiteralVar.create(index) + return cls( + _js_expr="", + _var_type=_determine_value_of_array_index( + array._var_type, + index_var._var_value + if isinstance(index_var, LiteralNumberVar) + else None, + ), + _var_data=_var_data, + _array=array, + _index=index_var, + ) + @var_operation def array_range_operation( diff --git a/reflex/istate/proxy.py b/reflex/istate/proxy.py index 6b5b7b75540..4ad096692cf 100644 --- a/reflex/istate/proxy.py +++ b/reflex/istate/proxy.py @@ -1205,7 +1205,7 @@ def __delattr__(self, name: str): """ self._mark_dirty(super().__delattr__, args=(name,)) - def __delitem__(self, key: str): + def __delitem__(self, key: Any): """Delete the item on the proxied object and mark state dirty. Args: @@ -1213,7 +1213,7 @@ def __delitem__(self, key: str): """ self._mark_dirty(super().__delitem__, args=(key,)) # pyright: ignore[reportAttributeAccessIssue] - def __setitem__(self, key: str, value: Any): + def __setitem__(self, key: Any, value: Any): """Set the item on the proxied object and mark state dirty. Args: diff --git a/reflex/state.py b/reflex/state.py index 51934fe3e97..bdec7fd9082 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -10,11 +10,21 @@ import inspect import json import logging +import math import operator import pickle import re import sys -from collections.abc import Callable, Iterable, Iterator, Sequence +from collections.abc import ( + Callable, + Iterable, + Iterator, + Mapping, + MutableMapping, + MutableSequence, + Sequence, +) +from collections.abc import Set as AbstractSet from datetime import timedelta from hashlib import md5 from types import FunctionType @@ -56,6 +66,7 @@ StateSerializationError, StateTooLargeError, UnretrievableVarValueError, + UnsettableVarValueError, ) from reflex_base.utils.exceptions import ImmutableStateError as ImmutableStateError from reflex_base.utils.serializers import serializer @@ -65,7 +76,6 @@ ComputedVar, DynamicRouteVar, EvenMoreBasicBaseState, - ToOperation, Var, _declared_value, _inherited_value, @@ -77,6 +87,8 @@ dispatch, is_computed_var, ) +from reflex_base.vars.object import var_access_path +from reflex_base.vars.set import _ordered from rich.markup import escape import reflex.istate.dynamic @@ -417,6 +429,61 @@ def _bind_attr(cls: type, name: str, value: Any) -> None: value.__set_name__(cls, name) +_UNSET = object() + + +def _array_index(key: Any) -> int | None: + """Get the index ``Array.prototype.at`` reads for a key. + + It truncates a number toward zero and reads NaN as 0. + + Args: + key: The key. + + Returns: + The index, or None for a key that is no finite number. + """ + if isinstance(key, int): + return key + if isinstance(key, float): + if math.isnan(key): + return 0 + return int(key) if math.isfinite(key) else None + return None + + +def _read_key(value: Any, key: Any, from_literal: bool = False) -> Any: + """Read a key off a value the way the frontend reads it with optional chaining. + + A set reads as the array the frontend gets: sorted where it can be if it is + in a literal, which renders it that way, and in its own order if it is held + in state, which serializes it that way. + + Args: + value: The value to read from. + key: The attribute name, mapping key or sequence index. + from_literal: Whether the value is read from a literal var. + + Returns: + The value at the key, or None if the key is missing or the value is None. + """ + if value is None: + return None + # A MutableProxy reports the class it wraps. Testing that class skips the + # failed check of the proxy's own type that isinstance makes first. + cls = value.__class__ + if issubclass(cls, Mapping): + return value.get(key) + if issubclass(cls, AbstractSet): + value = _ordered(value) if from_literal else tuple(value) + elif not issubclass(cls, Sequence): + return getattr(value, key, None) if isinstance(key, str) else None + index = _array_index(key) + if index is None or not -len(value) <= index < len(value): + return None + return value[index] + + def _router_fget(self: BaseState) -> RouterData: """Assemble the RouterData view over the per-field router vars. @@ -1964,8 +2031,39 @@ async def get_state(self, state_cls: type[T_STATE]) -> T_STATE: # Slow case - fetch missing parent states from redis. return await self._get_state_from_redis(state_cls) + async def _get_field_owner(self, root: Var) -> tuple[BaseState, str] | None: + """Get the state holding the field a root var stands for, and its name. + + Args: + root: A var as ``var_access_path`` splits it off. + + Returns: + The state and the field name, or None if the var is no state field. + """ + # The var's *own* var data, not the recursive _get_all_var_data(): an + # operation such as State.a + State.b merges in the var data of its + # operands, so it would pass for State.a. Only a plain field or + # computed var reference carries state + field_name on its own. + var_data = root._var_data + if var_data is None: + return None + state_name, field_name = var_data.state, var_data.field_name + if not state_name or not field_name: + return None + if state_name == self.get_full_name(): + return self, field_name + other_state = await self.get_state( + self._get_root_state().get_class_substate(state_name) + ) + return other_state, field_name + async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE: - """Get the value of an rx.Var from another state. + """Get the value of an rx.Var, possibly from another state. + + Besides a state field or computed var, the var may read into one by + attribute access, item access or array indexing, as in + ``State.user.tags[0]`` or ``State.mapping[State.key]``. Like on the + frontend, a missing key or a None on the way reads as None. Args: var: The var to get the value for. @@ -1980,44 +2078,105 @@ async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE: # Oopsie case: you didn't give me a Var... so get what you give. if not isinstance(var, Var): return var + value, _ = await self._resolve_var_value(var) + return value - unset = object() + async def _resolve_var_value(self, var: Var) -> tuple[Any, bool]: + """Resolve a var to its value, and whether that value is read from a literal. - # Fast case: this is a literal var and the value is known. - if ( - var_value := getattr(var, "_var_value", unset) - ) is not unset and not isinstance(var_value, Var): - return var_value # pyright: ignore [reportReturnType] - - # Unwrap any cast wrappers and resolve via the underlying var's *own* - # var data, not the recursive _get_all_var_data(). For an operation or - # derived var (e.g. State.a + State.b or State.items[0]), the recursive - # merge back-fills state/field_name from the first operand, which would - # make us silently return that operand's value instead of the operation's - # result. Only a plain field or computed var reference carries - # state + field_name on its own var data. - inner_var = var - while isinstance(inner_var, ToOperation): - inner_var = inner_var._original - var_data = inner_var._var_data - if var_data is None or not var_data.state or not var_data.field_name: - msg = f"Unable to retrieve value for {var._js_expr}: not associated with any state." - raise UnretrievableVarValueError(msg) - # Fastish case: this var belongs to this state - if var_data.state == self.get_full_name(): - value = getattr(self, var_data.field_name) + Args: + var: The var to resolve. + + Returns: + The value, and whether it is read from a literal var. + + Raises: + UnretrievableVarValueError: If the var does not have a literal value + or associated state. + """ + root, keys = var_access_path(var) + # Fast case: the var reads from a literal whose value is known. + value: Any = getattr(root, "_var_value", _UNSET) + from_literal = value is not _UNSET and not isinstance(value, Var) + if not from_literal: + owner = await self._get_field_owner(root) + if owner is None: + msg = f"Unable to retrieve value for {var._js_expr}: not associated with any state." + raise UnretrievableVarValueError(msg) + state, field_name = owner + value = getattr(state, field_name) if inspect.isawaitable(value): - return await value - return value + value = await value + for key in keys: + if isinstance(key, Var): + key = await self.get_var_value(key) + value = _read_key(value, key, from_literal) + # A literal container holds vars, which the frontend reads as their values. + if isinstance(value, Var): + value, from_literal = await self._resolve_var_value(value) + return value, from_literal + + async def set_var_value(self, var: Var[VAR_TYPE], value: VAR_TYPE) -> None: + """Set the value of an rx.Var, possibly in another state. + + The counterpart of ``get_var_value``: the var may be a state field or + read into one, as in ``State.user.tags[0]``. A nested value is set + through the field, which marks the field dirty. - # Slow case: this var belongs to another state - other_state = await self.get_state( - self._get_root_state().get_class_substate(var_data.state) - ) - value = getattr(other_state, var_data.field_name) - if inspect.isawaitable(value): - return await value - return value + Args: + var: The var to set. + value: The value to set. + + Raises: + UnsettableVarValueError: If the var is no state field and reads into + none, if it reads into a computed var, or if the object to set + the value on is None, is not tracked for changes, or takes no + item at the key. + """ + root, keys = var_access_path(var) + owner = await self._get_field_owner(root) + if owner is None: + msg = f"Unable to set value for {var._js_expr}: not associated with any state." + raise UnsettableVarValueError(msg) + state, field_name = owner + if field_name in state.computed_vars: + msg = ( + f"Unable to set value for {var._js_expr}: computed vars are read-only." + ) + raise UnsettableVarValueError(msg) + if not keys: + setattr(state, field_name, value) + return + + *parent_keys, last_key = [ + await self.get_var_value(key) if isinstance(key, Var) else key + for key in keys + ] + target = getattr(state, field_name) + for key in parent_keys: + target = _read_key(target, key) + if target is None: + msg = f"Unable to set value for {var._js_expr}: the object to set it on is None." + raise UnsettableVarValueError(msg) + # Only a change made through a MutableProxy marks the field dirty: one + # made to an object inside a tuple, for instance, reaches no frontend. + if not isinstance(target, MutableProxy): + msg = f"Unable to set value for {var._js_expr}: the object to set it on is not tracked for changes." + raise UnsettableVarValueError(msg) + if isinstance(target, MutableMapping): + target[last_key] = value + elif isinstance(target, MutableSequence): + # Checked first, as MutableProxy marks the field dirty before it assigns. + index = _array_index(last_key) + if index is None or not -len(target) <= index < len(target): + msg = f"Unable to set value for {var._js_expr}: {last_key!r} is not an index of its {len(target)} items." + raise UnsettableVarValueError(msg) + target[index] = value + elif isinstance(last_key, str): + setattr(target, last_key, value) + else: + msg = f"Unable to set value for {var._js_expr}: {target.__class__.__name__} values do not support item assignment." + raise UnsettableVarValueError(msg) def _mark_dirty_computed_vars( self, var_names: Iterable[str] | None = None, *, invalidate: bool = True diff --git a/tests/benchmarks/test_var_operations.py b/tests/benchmarks/test_var_operations.py index 3a525727871..f0de9b6a9fd 100644 --- a/tests/benchmarks/test_var_operations.py +++ b/tests/benchmarks/test_var_operations.py @@ -302,7 +302,7 @@ def test_array_index_operation(benchmark: BenchmarkFixture): """Benchmark building array indexing, the hot operation in a foreach body. Kept apart from the other array operations because it is built differently: - ``array_item_operation`` renders its operands with ``!s``, which calls + ``ArrayItemOperation`` renders its operands with ``!s``, which calls ``str()`` rather than ``__format__``, so indexing never interpolates an operand the way the rest of the family does. @@ -320,9 +320,9 @@ def _(): def test_array_range_operation(benchmark: BenchmarkFixture): """Benchmark building ``ArrayVar.range``, which backs a counted foreach. - The sibling of ``test_array_index_operation``: ``array_range_operation`` is - the other ``@var_operation`` rendering its operands with ``!s``, so it is - built without interpolating one either. + The sibling of ``test_array_index_operation``: ``array_range_operation`` + renders its operands with ``!s`` too, so it is built without interpolating + one either. Args: benchmark: The codspeed benchmark fixture. diff --git a/tests/benchmarks/test_var_value.py b/tests/benchmarks/test_var_value.py new file mode 100644 index 00000000000..1271523a8f9 --- /dev/null +++ b/tests/benchmarks/test_var_value.py @@ -0,0 +1,121 @@ +"""Benchmarks for resolving and setting vars with get_var_value and set_var_value. + +A handler that is handed a var, rather than naming an attribute itself, goes +through these: a form field bound to ``State.people[0].address.city`` reads and +writes the value its var points at on every change. Each routine resolves a var +of one shape a fixed number of times in a single coroutine: a plain field, an +attribute read through an array item, a read whose index is a var, and writes +matching the first two, the nested one going through the field's MutableProxy. + +The vars are built outside the measured body, as a handler receives them ready. +""" + +import asyncio +import dataclasses +from collections.abc import Callable, Coroutine +from typing import Any + +import pytest +from pytest_codspeed import BenchmarkFixture + +import reflex as rx +from reflex.state import BaseState + +# How many times each routine resolves its var. A nested read costs a few +# microseconds uninstrumented, so even the plain field read keeps every measured +# call above the ~100 microseconds the benchmark README asks for. +N = 1_000 + + +@dataclasses.dataclass +class Address: + """The innermost object the nested vars read into.""" + + city: str = "Berlin" + + +@dataclasses.dataclass +class Person: + """An array item holding an object and a list.""" + + address: Address = dataclasses.field(default_factory=Address) + tags: list[str] = dataclasses.field(default_factory=lambda: ["a", "b"]) + + +class VarValueState(BaseState): + """The state whose vars the routines resolve.""" + + count: rx.Field[int] = rx.field(0) + index: rx.Field[int] = rx.field(0) + people: rx.Field[list[Person]] = rx.field(default_factory=lambda: [Person()]) + + +FIELD = VarValueState.count +NESTED = VarValueState.people[0].address.city +VAR_KEY = VarValueState.people[VarValueState.index].tags.to(list[str])[1] + + +async def _get_field(state: VarValueState) -> None: + for _ in range(N): + await state.get_var_value(FIELD) + + +async def _get_nested(state: VarValueState) -> None: + for _ in range(N): + await state.get_var_value(NESTED) + + +async def _get_var_key(state: VarValueState) -> None: + for _ in range(N): + await state.get_var_value(VAR_KEY) + + +async def _set_field(state: VarValueState) -> None: + for i in range(N): + await state.set_var_value(FIELD, i) + + +async def _set_nested(state: VarValueState) -> None: + for _ in range(N): + await state.set_var_value(NESTED, "Hamburg") + + +@pytest.fixture( + params=[ + pytest.param(_get_field, id="get_field"), + pytest.param(_get_nested, id="get_nested"), + pytest.param(_get_var_key, id="get_var_key"), + pytest.param(_set_field, id="set_field"), + pytest.param(_set_nested, id="set_nested"), + ] +) +def routine( + request: pytest.FixtureRequest, +) -> Callable[[VarValueState], Coroutine[Any, Any, None]]: + """A parametrized get_var_value or set_var_value routine. + + Args: + request: The pytest fixture request carrying the routine. + + Returns: + The routine to benchmark. + """ + return request.param + + +def test_var_value( + routine: Callable[[VarValueState], Coroutine[Any, Any, None]], + benchmark: BenchmarkFixture, +): + """Benchmark resolving or setting one shape of var on a state. + + Args: + routine: The parametrized routine. + benchmark: The codspeed benchmark fixture. + """ + state = VarValueState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + loop = asyncio.new_event_loop() + try: + benchmark(lambda: loop.run_until_complete(routine(state))) + finally: + loop.close() diff --git a/tests/units/reflex_base/vars/test_base.py b/tests/units/reflex_base/vars/test_base.py index 2be7bd6e83d..6b0e4c81fa2 100644 --- a/tests/units/reflex_base/vars/test_base.py +++ b/tests/units/reflex_base/vars/test_base.py @@ -76,6 +76,7 @@ from reflex_base.vars.number import NumberVar from reflex_base.vars.object import LiteralObjectVar, ObjectItemOperation, ObjectVar from reflex_base.vars.sequence import ( + ArrayItemOperation, ArraySliceOperation, ArrayVar, ConcatVarOperation, @@ -2939,6 +2940,7 @@ def names(classes: Iterable[type]) -> set[str]: assert names(operations) == names({ CustomVarOperation, + ArrayItemOperation, ArraySliceOperation, ObjectItemOperation, ConcatVarOperation, diff --git a/tests/units/reflex_base/vars/test_object.py b/tests/units/reflex_base/vars/test_object.py new file mode 100644 index 00000000000..12e72889be1 --- /dev/null +++ b/tests/units/reflex_base/vars/test_object.py @@ -0,0 +1,59 @@ +import dataclasses + +from reflex_base.vars.base import Var +from reflex_base.vars.object import ObjectVar, var_access_path + + +@dataclasses.dataclass +class Tagged: + """An object with an attribute to read into.""" + + tags: list[str] = dataclasses.field(default_factory=list) + + +def test_var_access_path_follows_attribute_item_and_index_access() -> None: + """Attribute, item and index access walk down through casts to the var read from.""" + rows = Var(_js_expr="state.rows") + mapping = Var(_js_expr="state.mapping") + + root, keys = var_access_path(rows.to(list[Tagged])[0].tags.to(list[str])[-1]) + assert root is rows + assert keys == (0, "tags", -1) + + root, keys = var_access_path( + mapping.to(dict[int, Tagged])[1].to(ObjectVar, Tagged).tags + ) + assert root is mapping + assert keys == (1, "tags") + + root, keys = var_access_path(rows.to(list[int])) + assert root is rows + assert keys == () + + +def test_var_access_path_keeps_keys_that_are_vars() -> None: + """A key or index that is no literal is kept as its var.""" + rows = Var(_js_expr="state.rows") + index = Var(_js_expr="state.index").to(int) + key = Var(_js_expr="state.key").to(str) + + root, keys = var_access_path(rows.to(list[dict[str, int]])[index][key]) + + assert root is rows + assert len(keys) == 2 + assert keys[0] is index + assert keys[1] is key + + +def test_var_access_path_stops_at_other_operations() -> None: + """Any other operation is the root, with the keys read off its result.""" + n = Var(_js_expr="state.n").to(int) + items = Var(_js_expr="state.items").to(list[int]) + + root, keys = var_access_path(n + 1) + assert str(root) == "(state.n + 1)" + assert keys == () + + root, keys = var_access_path(items.reverse()[0]) + assert str(root) == str(items.reverse()) + assert keys == (0,) diff --git a/tests/units/reflex_base/vars/test_sequence.py b/tests/units/reflex_base/vars/test_sequence.py index b28ae44baae..5dcd3dcb8f5 100644 --- a/tests/units/reflex_base/vars/test_sequence.py +++ b/tests/units/reflex_base/vars/test_sequence.py @@ -66,6 +66,17 @@ def test_optional_array_operation_results(): assert str(maybe.is_none()) == "!(isNotNullOrUndefined(state.maybe))" +def test_array_index_renders_the_index_as_javascript(): + """An int, a bool and a var index each render as the JavaScript they stand for.""" + items = _array(list[str], "state.items") + index = Var(_js_expr="state.index").to(int) + + assert str(items[-1]) == "state.items?.at?.(-1)" + assert str(items[True]) == "state.items?.at?.(true)" + assert str(items[index]) == "state.items?.at?.(state.index)" + assert items[0]._var_type is str + + _COMPARE_ARRAYS = ImportVar(tag="compareArrays", is_default=True, install=False) _COMPARE_DATETIME = ImportVar(tag="compareDatetime", is_default=True, install=False) diff --git a/tests/units/test_state.py b/tests/units/test_state.py index 4a516554069..39406daef44 100644 --- a/tests/units/test_state.py +++ b/tests/units/test_state.py @@ -39,7 +39,7 @@ from reflex_base.event.context import EventContext from reflex_base.event.processor import BaseStateEventProcessor from reflex_base.registry import RegistrationContext -from reflex_base.utils import format, types +from reflex_base.utils import format, serializers, types from reflex_base.utils.exceptions import ( EventHandlerValueError, InvalidLockWarningThresholdError, @@ -51,6 +51,7 @@ StateTooLargeError, StateValueError, UnretrievableVarValueError, + UnsettableVarValueError, ) from reflex_base.utils.format import json_dumps from reflex_base.vars.base import Field, Var, computed_var, field @@ -6666,21 +6667,200 @@ async def test_get_var_value( } # Regression for https://github.com/reflex-dev/reflex/issues/6629: a Var - # operation / derived var (arithmetic, indexed or item access) must not - # silently return the value of its first constituent field. Such vars have - # no retrievable value, so raise instead of returning a plausible-but-wrong one. + # operation must not silently return the value of its first constituent + # field. Arithmetic has no retrievable value, so it raises instead of + # returning a plausible-but-wrong one; indexed and item access resolve to + # the value they read. with pytest.raises(UnretrievableVarValueError): await state.get_var_value(TestState.num1 + TestState.num2) - with pytest.raises(UnretrievableVarValueError): - # array[0] is a Var operation at runtime, though statically typed as the element. - await state.get_var_value(TestState.array[0]) # pyright: ignore[reportArgumentType] - with pytest.raises(UnretrievableVarValueError): - await state.get_var_value(TestState.mapping["a"]) + # array[0] is a Var operation at runtime, though statically typed as the element. + assert await state.get_var_value(TestState.array[0]) == 1 # pyright: ignore[reportArgumentType] + assert await state.get_var_value(TestState.mapping["a"]) == [1, 2, 3] # Computed vars are derived but state-bound, so they remain resolvable. assert await state.get_var_value(TestState.sum) == pytest.approx(42 + 3.15) +@pytest.mark.asyncio +async def test_get_var_value_reads_into_fields( + state_manager: StateManager, substate_token: BaseStateToken +): + """A var reading into a field resolves to what it reads, as on the frontend. + + Args: + state_manager: The state manager to use. + substate_token: Token for the substate used by state_manager. + """ + state = await state_manager.get_state(substate_token) + assert isinstance(state, TestState) + + assert await state.get_var_value(TestState.obj.prop2) == "hello" # pyright: ignore[reportArgumentType] + assert await state.get_var_value(TestState.complex[2].prop1) == 42 # pyright: ignore[reportArgumentType] + assert await state.get_var_value(TestState.mapping["b"][-1]) == 6 + # A key that is a var is resolved first. + assert await state.get_var_value(TestState.mapping[TestState.map_key]) == [1, 2, 3] # pyright: ignore[reportArgumentType] + # A literal resolves without any state, and a var it holds to its value. + assert await state.get_var_value(rx.Var.create({"a": [1, 2]})["a"][1]) == 2 + held = rx.Var.create([TestState.mapping])[0].to(dict[str, list[int]]) + assert await state.get_var_value(held["b"]) == [4, 5, 6] + # A literal renders a set sorted, so its indexes read in that order. + assert await state.get_var_value(rx.Var.create({8, 1}).to_array()[0]) == 1 + # A missing key reads as None, and so does everything read past it. + assert await state.get_var_value(TestState.array[3]) is None # pyright: ignore[reportArgumentType] + assert await state.get_var_value(TestState.mapping["c"][0]) is None + + child_state = await state.get_state(ChildState) + child_state.mapping = {"a": [7]} + assert await state.get_var_value(ChildState.mapping["a"][0]) == 7 + + +@pytest.mark.asyncio +async def test_set_var_value( + state_manager: StateManager, substate_token: BaseStateToken +): + """set_var_value sets what get_var_value reads and marks the field dirty. + + Args: + state_manager: The state manager to use. + substate_token: Token for the substate used by state_manager. + """ + state = await state_manager.get_state(substate_token) + assert isinstance(state, TestState) + state._clean() + + await state.set_var_value(TestState.num1, 7) + assert state.num1 == 7 + assert state.dirty_vars == {"num1", "sum"} + + state._clean() + await state.set_var_value(TestState.obj.prop1, 7) # pyright: ignore[reportArgumentType] + assert state.obj.prop1 == 7 + assert state.dirty_vars == {"obj"} + + state._clean() + await state.set_var_value(TestState.mapping[TestState.map_key][0], 7) # pyright: ignore[reportArgumentType] + assert state.mapping == {"a": [7, 2, 3], "b": [4, 5, 6]} + assert state.dirty_vars == {"mapping"} + + child_state = await state.get_state(ChildState) + child_state._clean() + await state.set_var_value(ChildState.count, 7) + assert child_state.count == 7 + assert child_state.dirty_vars == {"count"} + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("var", "reason"), + [ + (TestState.sum, "computed vars are read-only"), + (Var(_js_expr="undefined"), "not associated with any state"), + (TestState.num1 + TestState.num2, "not associated with any state"), + (TestState.mapping["c"][0], "the object to set it on is None"), + ], + ids=["computed", "no_state", "operation", "none_on_the_way"], +) +async def test_set_var_value_rejects_what_it_cannot_set( + var: Var, + reason: str, + state_manager: StateManager, + substate_token: BaseStateToken, +): + """set_var_value raises for a var it cannot set, naming the var and why. + + Args: + var: The var to set. + reason: Why the var cannot be set. + state_manager: The state manager to use. + substate_token: Token for the substate used by state_manager. + """ + state = await state_manager.get_state(substate_token) + + with pytest.raises(UnsettableVarValueError) as err: + await state.set_var_value(var, 0) + + assert str(err.value) == f"Unable to set value for {var._js_expr}: {reason}." + + +@pytest.mark.asyncio +async def test_var_value_through_tuples_and_sets() -> None: + """Tuples and sets read like the arrays they are sent as, but are not set through. + + Neither is wrapped in a MutableProxy, so a change made through one would mark + no field dirty and reach no frontend; set_var_value refuses it unchanged. + """ + + class TupleAndSetState(BaseState): + rows: rx.Field[tuple[list[int], ...]] = rx.field(default_factory=lambda: ([1],)) + tags: rx.Field[set[str]] = rx.field(default_factory=lambda: {"x"}) + numbers: rx.Field[set[int]] = rx.field(default_factory=lambda: {8, 1}) + + state = TupleAndSetState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + row_item = TupleAndSetState.rows[0][0] + tag = TupleAndSetState.tags.to_array()[0] + + assert await state.get_var_value(row_item) == 1 + assert await state.get_var_value(tag) == "x" + # State sends a set in its own order, unlike a literal, which sorts it. + first_number = await state.get_var_value(TupleAndSetState.numbers.to_array()[0]) + assert first_number == serializers.serialize_set(state.get_value("numbers"))[0] + + with pytest.raises(UnsettableVarValueError) as err: + await state.set_var_value(row_item, 7) + assert str(err.value) == ( + f"Unable to set value for {row_item._js_expr}: " + "the object to set it on is not tracked for changes." + ) + with pytest.raises(UnsettableVarValueError) as err: + await state.set_var_value(tag, "y") + assert str(err.value) == ( + f"Unable to set value for {tag._js_expr}: " + "set values do not support item assignment." + ) + assert state.rows == ([1],) + assert state.tags == {"x"} + assert state.dirty_vars == set() + + +@pytest.mark.asyncio +async def test_var_value_indexes_like_array_at() -> None: + """An index reads and sets the item ``Array.prototype.at`` reads on the frontend. + + It truncates a fractional index toward zero and reads NaN as 0. An index + past either end reads as None, and set_var_value refuses it before anything + is marked dirty. + """ + + class IndexState(BaseState): + index: rx.Field[int | float] = rx.field(1.5) + items: rx.Field[list[str]] = rx.field(default_factory=lambda: ["a", "b", "c"]) + + state = IndexState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] + item = IndexState.items[IndexState.index] + + assert await state.get_var_value(item) == "b" + state.index = -1.5 + assert await state.get_var_value(item) == "c" + state.index = math.nan + assert await state.get_var_value(item) == "a" + state.index = math.inf + assert await state.get_var_value(item) is None + + state.index = 1.5 + await state.set_var_value(item, "x") + assert state.items == ["a", "x", "c"] + + state._clean() + past_end = IndexState.items[3] + with pytest.raises(UnsettableVarValueError) as err: + await state.set_var_value(past_end, "y") + assert str(err.value) == ( + f"Unable to set value for {past_end._js_expr}: 3 is not an index of its 3 items." + ) + assert state.items == ["a", "x", "c"] + assert state.dirty_vars == set() + + @pytest.mark.asyncio async def test_get_var_value_async_computed_var( token: str, attached_mock_event_context: EventContext