From 16075aed870e7288c93250316781072051f5587c Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 5 Feb 2025 02:19:11 +0100 Subject: [PATCH] throw error if computed var has args --- reflex/utils/exceptions.py | 15 +++++++++++++++ reflex/vars/base.py | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index 05fbb297c..7793415ed 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -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.""" diff --git a/reflex/vars/base.py b/reflex/vars/base.py index d34bc8ff5..da6196307 100644 --- a/reflex/vars/base.py +++ b/reflex/vars/base.py @@ -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: