-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: async default setters break setvar #4169
fix: async default setters break setvar #4169
Conversation
Yes, that's what bit us. |
We could also try to implement async setvar instead. Or disallow overwriting default setter names either completely or with async functions. @reflex-dev wdyt? |
Could it be this simple? diff --git a/reflex/state.py b/reflex/state.py
index 51fd740df..65d95c53f 100644
--- a/reflex/state.py
+++ b/reflex/state.py
@@ -191,7 +191,7 @@ class EventHandlerSetVar(EventHandler):
)
object.__setattr__(self, "state_cls", state_cls)
- def setvar(self, var_name: str, value: Any):
+ async def setvar(self, var_name: str, value: Any):
"""Set the state variable to the value of the event.
Note: `self` here will be an instance of the state, not EventHandlerSetVar.
@@ -200,7 +200,10 @@ class EventHandlerSetVar(EventHandler):
var_name: The name of the variable to set.
value: The value to set the variable to.
"""
- getattr(self, constants.SETTER_PREFIX + var_name)(value)
+ res = getattr(self, constants.SETTER_PREFIX + var_name)(value)
+ if hasattr(res, "__await__"):
+ return await res
+ return res
def __call__(self, *args: Any) -> EventSpec:
"""Performs pre-checks and munging on the provided args that will become an EventSpec.
@@ -214,7 +217,6 @@ class EventHandlerSetVar(EventHandler):
Raises:
AttributeError: If the given Var name does not exist on the state.
EventHandlerValueError: If the given Var name is not a str
- NotImplementedError: If the setter for the given Var is async
"""
from reflex.utils.exceptions import EventHandlerValueError
@@ -232,11 +234,6 @@ class EventHandlerSetVar(EventHandler):
f"Variable `{args[0]}` cannot be set on `{self.state_cls.get_full_name()}`"
)
- if asyncio.iscoroutinefunction(handler.fn):
- raise NotImplementedError(
- f"Setter for {args[0]} is async, which is not supported."
- )
-
return super().__call__(*args)
|
Thanks, @masenf, yes it could be that simple, maybe with However, this will change the setvar api to async, which seems like a bad idea |
I'm not sure i see the problem with this API being async... it's really only used to create frontend events that set vars.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
taking this for 0.6.4, we can figure out async setters later
No description provided.