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:
parent
8e66c9b3c7
commit
44d27f9e4c
@ -828,6 +828,8 @@ class App(LifespanMixin, Base):
|
|||||||
state = self.state
|
state = self.state
|
||||||
|
|
||||||
for var in state.computed_vars.values():
|
for var in state.computed_vars.values():
|
||||||
|
if not var._cache:
|
||||||
|
continue
|
||||||
deps = var._deps(objclass=state)
|
deps = var._deps(objclass=state)
|
||||||
for dep in deps:
|
for dep in deps:
|
||||||
if dep not in state.vars and dep not in state.backend_vars:
|
if dep not in state.vars and dep not in state.backend_vars:
|
||||||
|
@ -36,7 +36,12 @@ from typing import (
|
|||||||
from reflex import constants
|
from reflex import constants
|
||||||
from reflex.base import Base
|
from reflex.base import Base
|
||||||
from reflex.utils import console, imports, serializers, types
|
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
|
# This module used to export ImportVar itself, so we still import it for export here
|
||||||
from reflex.utils.imports import (
|
from reflex.utils.imports import (
|
||||||
@ -2233,10 +2238,14 @@ def computed_var(
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: If caching is disabled and an update interval is set.
|
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:
|
if cache is False and interval is not None:
|
||||||
raise ValueError("Cannot set update interval without caching.")
|
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:
|
if fget is not None:
|
||||||
return ComputedVar(fget=fget, cache=cache)
|
return ComputedVar(fget=fget, cache=cache)
|
||||||
|
|
||||||
|
@ -1548,11 +1548,11 @@ def test_app_with_valid_var_dependencies(compilable_app: tuple[App, Path]):
|
|||||||
base: int = 0
|
base: int = 0
|
||||||
_backend: int = 0
|
_backend: int = 0
|
||||||
|
|
||||||
@computed_var
|
@computed_var(cache=True)
|
||||||
def foo(self) -> str:
|
def foo(self) -> str:
|
||||||
return "foo"
|
return "foo"
|
||||||
|
|
||||||
@computed_var(deps=["_backend", "base", foo])
|
@computed_var(deps=["_backend", "base", foo], cache=True)
|
||||||
def bar(self) -> str:
|
def bar(self) -> str:
|
||||||
return "bar"
|
return "bar"
|
||||||
|
|
||||||
@ -1564,7 +1564,7 @@ def test_app_with_invalid_var_dependencies(compilable_app: tuple[App, Path]):
|
|||||||
app, _ = compilable_app
|
app, _ = compilable_app
|
||||||
|
|
||||||
class InvalidDepState(BaseState):
|
class InvalidDepState(BaseState):
|
||||||
@computed_var(deps=["foolksjdf"])
|
@computed_var(deps=["foolksjdf"], cache=True)
|
||||||
def bar(self) -> str:
|
def bar(self) -> str:
|
||||||
return "bar"
|
return "bar"
|
||||||
|
|
||||||
|
@ -1406,6 +1406,7 @@ def cv_fget(state: BaseState) -> int:
|
|||||||
def test_computed_var_deps(deps: List[Union[str, Var]], expected: Set[str]):
|
def test_computed_var_deps(deps: List[Union[str, Var]], expected: Set[str]):
|
||||||
@computed_var(
|
@computed_var(
|
||||||
deps=deps,
|
deps=deps,
|
||||||
|
cache=True,
|
||||||
)
|
)
|
||||||
def test_var(state) -> int:
|
def test_var(state) -> int:
|
||||||
return 1
|
return 1
|
||||||
@ -1426,6 +1427,7 @@ def test_invalid_computed_var_deps(deps: List):
|
|||||||
|
|
||||||
@computed_var(
|
@computed_var(
|
||||||
deps=deps,
|
deps=deps,
|
||||||
|
cache=True,
|
||||||
)
|
)
|
||||||
def test_var(state) -> int:
|
def test_var(state) -> int:
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
Reference in New Issue
Block a user