Explorar el Código

throw error if computed var has args (#4753)

* throw error if computed var has args

* change message
Thomas Brandého hace 3 meses
padre
commit
59d8d1eb62
Se han modificado 2 ficheros con 19 adiciones y 0 borrados
  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.")
 
 
+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):
     """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.utils import console, exceptions, imports, serializers, types
 from reflex.utils.exceptions import (
+    ComputedVarSignatureError,
     UntypedComputedVarError,
     VarAttributeError,
     VarDependencyError,
@@ -2602,6 +2603,7 @@ def computed_var(
     Raises:
         ValueError: If caching is disabled and an update interval is set.
         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:
         raise ValueError("Cannot set update interval without caching.")
@@ -2610,6 +2612,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))
+
         if inspect.iscoroutinefunction(fget):
             computed_var_cls = AsyncComputedVar
         else: