diff --git a/reflex/app.py b/reflex/app.py index e06956b20..e0a02062e 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -828,6 +828,8 @@ class App(LifespanMixin, Base): state = self.state for var in state.computed_vars.values(): + if not var._cache: + continue deps = var._deps(objclass=state) for dep in deps: if dep not in state.vars and dep not in state.backend_vars: diff --git a/reflex/vars.py b/reflex/vars.py index 4266488d5..ebe37abbf 100644 --- a/reflex/vars.py +++ b/reflex/vars.py @@ -36,7 +36,12 @@ from typing import ( from reflex import constants from reflex.base import Base from reflex.utils import console, imports, serializers, types -from reflex.utils.exceptions import VarAttributeError, VarTypeError, VarValueError +from reflex.utils.exceptions import ( + VarAttributeError, + VarDependencyError, + VarTypeError, + VarValueError, +) # This module used to export ImportVar itself, so we still import it for export here from reflex.utils.imports import ( @@ -2233,10 +2238,14 @@ def computed_var( Raises: ValueError: If caching is disabled and an update interval is set. + VarDependencyError: If user supplies dependencies without caching. """ if cache is False and interval is not None: raise ValueError("Cannot set update interval without caching.") + if cache is False and (deps is not None or auto_deps is False): + raise VarDependencyError("Cannot track dependencies without caching.") + if fget is not None: return ComputedVar(fget=fget, cache=cache) diff --git a/tests/test_app.py b/tests/test_app.py index 02982a10d..2bf838c4b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1548,11 +1548,11 @@ def test_app_with_valid_var_dependencies(compilable_app: tuple[App, Path]): base: int = 0 _backend: int = 0 - @computed_var + @computed_var(cache=True) def foo(self) -> str: return "foo" - @computed_var(deps=["_backend", "base", foo]) + @computed_var(deps=["_backend", "base", foo], cache=True) def bar(self) -> str: return "bar" @@ -1564,7 +1564,7 @@ def test_app_with_invalid_var_dependencies(compilable_app: tuple[App, Path]): app, _ = compilable_app class InvalidDepState(BaseState): - @computed_var(deps=["foolksjdf"]) + @computed_var(deps=["foolksjdf"], cache=True) def bar(self) -> str: return "bar" diff --git a/tests/test_var.py b/tests/test_var.py index b57b6919a..e58daaa03 100644 --- a/tests/test_var.py +++ b/tests/test_var.py @@ -1406,6 +1406,7 @@ def cv_fget(state: BaseState) -> int: def test_computed_var_deps(deps: List[Union[str, Var]], expected: Set[str]): @computed_var( deps=deps, + cache=True, ) def test_var(state) -> int: return 1 @@ -1426,6 +1427,7 @@ def test_invalid_computed_var_deps(deps: List): @computed_var( deps=deps, + cache=True, ) def test_var(state) -> int: return 1