throw error if computed var has args (#4753)

* throw error if computed var has args

* change message
This commit is contained in:
Thomas Brandého 2025-02-05 18:23:41 +01:00 committed by Masen Furer
parent 810ead844d
commit 35954f749a
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95
2 changed files with 19 additions and 0 deletions

View File

@ -91,6 +91,19 @@ class UntypedComputedVarError(ReflexError, TypeError):
super().__init__(f"Computed var '{var_name}' must have a type annotation.") 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}` cannot take arguments.")
class MissingAnnotationError(ReflexError, TypeError): class MissingAnnotationError(ReflexError, TypeError):
"""Custom TypeError for missing annotations.""" """Custom TypeError for missing annotations."""

View File

@ -48,6 +48,7 @@ from reflex.base import Base
from reflex.constants.compiler import Hooks from reflex.constants.compiler import Hooks
from reflex.utils import console, exceptions, imports, serializers, types from reflex.utils import console, exceptions, imports, serializers, types
from reflex.utils.exceptions import ( from reflex.utils.exceptions import (
ComputedVarSignatureError,
UntypedComputedVarError, UntypedComputedVarError,
VarAttributeError, VarAttributeError,
VarDependencyError, VarDependencyError,
@ -2602,6 +2603,7 @@ 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. VarDependencyError: If user supplies dependencies without caching.
ComputedVarSignatureError: If the getter function has more than one argument.
""" """
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.")
@ -2610,6 +2612,10 @@ def computed_var(
raise VarDependencyError("Cannot track dependencies without caching.") raise VarDependencyError("Cannot track dependencies without caching.")
if fget is not None: if fget is not None:
sign = inspect.signature(fget)
if len(sign.parameters) != 1:
raise ComputedVarSignatureError(fget.__name__, signature=str(sign))
if inspect.iscoroutinefunction(fget): if inspect.iscoroutinefunction(fget):
computed_var_cls = AsyncComputedVar computed_var_cls = AsyncComputedVar
else: else: