throw error if computed var has args

This commit is contained in:
Lendemor 2025-02-05 02:19:11 +01:00
parent 8663dbcb97
commit 16075aed87
2 changed files with 20 additions and 0 deletions

View File

@ -91,6 +91,21 @@ class UntypedComputedVarError(ReflexError, TypeError):
super().__init__(f"Computed var '{var_name}' must have a type annotation.")
class ComputedVarSignatureError(ReflexError, TypeError):
"""Custom TypeError for computed var signature errors."""
def __init__(self, var_name: str, signature: str):
"""Initialize the ComputedVarSignatureError.
Args:
var_name: The name of the var.
signature: The invalid signature.
"""
super().__init__(
f"Computed var `{var_name}{signature}` must have a valid signature. It should only take one parameter: self."
)
class MissingAnnotationError(ReflexError, TypeError):
"""Custom TypeError for missing annotations."""

View File

@ -46,6 +46,7 @@ from reflex.base import Base
from reflex.constants.compiler import Hooks
from reflex.utils import console, exceptions, imports, serializers, types
from reflex.utils.exceptions import (
ComputedVarSignatureError,
UntypedComputedVarError,
VarAttributeError,
VarDependencyError,
@ -2388,6 +2389,10 @@ def computed_var(
raise VarDependencyError("Cannot track dependencies without caching.")
if fget is not None:
sign = inspect.signature(fget)
if len(sign.parameters) != 1:
raise ComputedVarSignatureError(fget.__name__, signature=str(sign))
return ComputedVar(fget, cache=cache)
def wrapper(fget: Callable[[BASE_STATE], Any]) -> ComputedVar: