diff --git a/reflex/state.py b/reflex/state.py index 89698cfa186..b65284899e0 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -796,6 +796,7 @@ def _handle_local_def(cls): cls.__module__ = reflex.istate.dynamic.__name__ @classmethod + @functools.cache def _get_type_hints(cls) -> dict[str, Any]: """Get the type hints for this class. @@ -898,8 +899,9 @@ def _check_overridden_basevars(cls): Raises: ComputedVarShadowsBaseVarsError: When a computed var shadows a base var. """ + hints = cls._get_type_hints() for name, computed_var_ in cls._get_computed_vars(): - if name in get_type_hints(cls): + if name in hints: msg = f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead" raise ComputedVarShadowsBaseVarsError(msg) diff --git a/reflex/utils/types.py b/reflex/utils/types.py index 455107b1b6d..b375157d556 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -57,6 +57,7 @@ StateVarTypes = (*PrimitiveTypes, Base, type(None)) if TYPE_CHECKING: + from reflex.state import BaseState from reflex.vars.base import Var VAR1 = TypeVar("VAR1", bound="Var") @@ -902,12 +903,12 @@ def is_valid_var_type(type_: type) -> bool: ) -def is_backend_base_variable(name: str, cls: type) -> bool: +def is_backend_base_variable(name: str, cls: type[BaseState]) -> bool: """Check if this variable name correspond to a backend variable. Args: name: The name of the variable to check - cls: The class of the variable to check + cls: The class of the variable to check (must be a BaseState subclass) Returns: bool: The result of the check @@ -924,11 +925,7 @@ def is_backend_base_variable(name: str, cls: type) -> bool: if name.startswith(f"_{cls.__name__}__"): return False - # Extract the namespace of the original module if defined (dynamic substates). - if callable(getattr(cls, "_get_type_hints", None)): - hints = cls._get_type_hints() - else: - hints = get_type_hints(cls) + hints = cls._get_type_hints() if name in hints: hint = get_origin(hints[name]) if hint == ClassVar: