Skip to content

Commit 34d77db

Browse files
authored
Fix event handler string args (#928)
1 parent d5977ff commit 34d77db

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

pynecone/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __call__(self, *args: Var) -> EventSpec:
6565

6666
# Otherwise, convert to JSON.
6767
try:
68-
values.append(Var.create(arg))
68+
values.append(Var.create(arg, is_string=type(arg) is str))
6969
except TypeError as e:
7070
raise TypeError(
7171
f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}."

pynecone/utils/format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ def format_cond(
223223

224224
# Format prop conds.
225225
if is_prop:
226-
prop1 = Var.create(true_value, is_string=type(true_value) == str)
227-
prop2 = Var.create(false_value, is_string=type(false_value) == str)
226+
prop1 = Var.create(true_value, is_string=type(true_value) is str)
227+
prop2 = Var.create(false_value, is_string=type(false_value) is str)
228228
assert prop1 is not None and prop2 is not None, "Invalid prop values"
229229
return f"{cond} ? {prop1} : {prop2}".replace("{", "").replace("}", "")
230230

tests/test_event.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,47 @@ def test_call_event_handler():
3434
def test_fn():
3535
pass
3636

37+
test_fn.__qualname__ = "test_fn"
38+
3739
def test_fn_with_args(_, arg1, arg2):
3840
pass
3941

42+
test_fn_with_args.__qualname__ = "test_fn_with_args"
43+
4044
handler = EventHandler(fn=test_fn)
4145
event_spec = handler()
4246

4347
assert event_spec.handler == handler
4448
assert event_spec.local_args == ()
4549
assert event_spec.args == ()
50+
assert format.format_event(event_spec) == 'E("test_fn", {})'
4651

4752
handler = EventHandler(fn=test_fn_with_args)
4853
event_spec = handler(make_var("first"), make_var("second"))
4954

55+
# Test passing vars as args.
5056
assert event_spec.handler == handler
5157
assert event_spec.local_args == ()
5258
assert event_spec.args == (("arg1", "first"), ("arg2", "second"))
59+
assert (
60+
format.format_event(event_spec)
61+
== 'E("test_fn_with_args", {arg1:first,arg2:second})'
62+
)
63+
64+
# Passing args as strings should format differently.
65+
event_spec = handler("first", "second") # type: ignore
66+
assert (
67+
format.format_event(event_spec)
68+
== 'E("test_fn_with_args", {arg1:"first",arg2:"second"})'
69+
)
5370

5471
first, second = 123, "456"
5572
handler = EventHandler(fn=test_fn_with_args)
5673
event_spec = handler(first, second) # type: ignore
74+
assert (
75+
format.format_event(event_spec)
76+
== 'E("test_fn_with_args", {arg1:123,arg2:"456"})'
77+
)
5778

5879
assert event_spec.handler == handler
5980
assert event_spec.local_args == ()

0 commit comments

Comments
 (0)