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

do not validate non-cached var deps #3576

Merged
merged 3 commits into from
Jun 28, 2024
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
2 changes: 2 additions & 0 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,8 @@ def _validate_var_dependencies(
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:
Expand Down
11 changes: 10 additions & 1 deletion reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
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 (
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,11 +1548,11 @@ class ValidDepState(BaseState):
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"

Expand All @@ -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"

Expand Down
2 changes: 2 additions & 0 deletions tests/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Loading