Просмотр исходного кода

throw error if computed var has args (#4753)

* throw error if computed var has args

* change message
Thomas Brandého 3 месяцев назад
Родитель
Сommit
35954f749a
2 измененных файлов с 19 добавлено и 0 удалено
  1. 13 0
      reflex/utils/exceptions.py
  2. 6 0
      reflex/vars/base.py

+ 13 - 0
reflex/utils/exceptions.py

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

+ 6 - 0
reflex/vars/base.py

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