do not validate non-cached var deps (#3576)

* do not validate non-cached var deps

* further improve Exceptions for misconfigured var deps
This commit is contained in:
benedikt-bartscher 2024-06-28 02:00:55 +02:00 committed by GitHub
parent 8e66c9b3c7
commit 44d27f9e4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 4 deletions

View File

@ -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:

View File

@ -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)

View File

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

View File

@ -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