Skip to content

Commit

Permalink
Do not allow call_function callback argument to be added afterwards (
Browse files Browse the repository at this point in the history
…#4552)

The default behavior for EventSpec is to treat the spec as a partial, wherein
if additional unfilled arguments are available, and the event trigger wants to
pass additional arguments, they will be applied positionally.

However, it never makes sense to fill in `callback` with an event trigger arg.
Therefore, if the callback is not provided initially, set it to None explicitly
so that some invalid value cannot be added later.
  • Loading branch information
masenf authored Jan 3, 2025
1 parent 8477a1a commit 316a0c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ def call_function(
Returns:
EventSpec: An event that will execute the client side javascript.
"""
callback_kwargs = {}
callback_kwargs = {"callback": None}
if callback is not None:
callback_kwargs = {
"callback": format.format_queue_events(
Expand Down
18 changes: 14 additions & 4 deletions tests/units/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,17 @@ def test_event_console_log():
)
assert (
format.format_event(spec)
== 'Event("_call_function", {function:(() => (console["log"]("message")))})'
== 'Event("_call_function", {function:(() => (console["log"]("message"))),callback:null})'
)
spec = event.console_log(Var(_js_expr="message"))
assert (
format.format_event(spec)
== 'Event("_call_function", {function:(() => (console["log"](message)))})'
== 'Event("_call_function", {function:(() => (console["log"](message))),callback:null})'
)
spec2 = event.console_log(Var(_js_expr="message2")).add_args(Var("throwaway"))
assert (
format.format_event(spec2)
== 'Event("_call_function", {function:(() => (console["log"](message2))),callback:null})'
)


Expand All @@ -243,12 +248,17 @@ def test_event_window_alert():
)
assert (
format.format_event(spec)
== 'Event("_call_function", {function:(() => (window["alert"]("message")))})'
== 'Event("_call_function", {function:(() => (window["alert"]("message"))),callback:null})'
)
spec = event.window_alert(Var(_js_expr="message"))
assert (
format.format_event(spec)
== 'Event("_call_function", {function:(() => (window["alert"](message)))})'
== 'Event("_call_function", {function:(() => (window["alert"](message))),callback:null})'
)
spec2 = event.window_alert(Var(_js_expr="message2")).add_args(Var("throwaway"))
assert (
format.format_event(spec2)
== 'Event("_call_function", {function:(() => (window["alert"](message2))),callback:null})'
)


Expand Down

0 comments on commit 316a0c9

Please sign in to comment.