Skip to content

Commit 60df907

Browse files
fail on using rxcond on event handlers or event spec (#5209)
* fail on using rxcond on event handlers or event spec * Add unit tests for EventVar and EventChainVar bool() methods Co-Authored-By: khaleel@reflex.dev <khaleel.aladhami@gmail.com> * Update test to verify event handlers fail when used in rx.cond() Co-Authored-By: khaleel@reflex.dev <khaleel.aladhami@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 9c51f67 commit 60df907

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

reflex/event.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Annotated,
1515
Any,
1616
Generic,
17+
NoReturn,
1718
Protocol,
1819
TypedDict,
1920
TypeVar,
@@ -1683,6 +1684,16 @@ def get_fn_signature(fn: Callable) -> inspect.Signature:
16831684
class EventVar(ObjectVar, python_types=(EventSpec, EventHandler)):
16841685
"""Base class for event vars."""
16851686

1687+
def bool(self) -> NoReturn:
1688+
"""Get the boolean value of the var.
1689+
1690+
Raises:
1691+
TypeError: EventVar cannot be converted to a boolean.
1692+
"""
1693+
raise TypeError(
1694+
f"Cannot convert {self._js_expr} of type {type(self).__name__} to bool."
1695+
)
1696+
16861697

16871698
@dataclasses.dataclass(
16881699
eq=False,
@@ -1759,6 +1770,16 @@ def no_args():
17591770
class EventChainVar(BuilderFunctionVar, python_types=EventChain):
17601771
"""Base class for event chain vars."""
17611772

1773+
def bool(self) -> NoReturn:
1774+
"""Get the boolean value of the var.
1775+
1776+
Raises:
1777+
TypeError: EventChainVar cannot be converted to a boolean.
1778+
"""
1779+
raise TypeError(
1780+
f"Cannot convert {self._js_expr} of type {type(self).__name__} to bool."
1781+
)
1782+
17621783

17631784
@dataclasses.dataclass(
17641785
eq=False,

tests/units/test_event.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,36 @@ def get_handler(self, arg: Var[str]):
485485
_ = rx.input(on_change=w.get_handler)
486486

487487

488+
def test_event_var_in_rx_cond():
489+
"""Test that EventVar and EventChainVar cannot be used in rx.cond()."""
490+
from reflex.components.core.cond import cond as rx_cond
491+
492+
class S(BaseState):
493+
@event
494+
def s(self):
495+
pass
496+
497+
handler_var = Var.create(S.s)
498+
with pytest.raises(TypeError) as err:
499+
rx_cond(handler_var, rx.text("True"), rx.text("False"))
500+
assert "Cannot convert" in str(err.value)
501+
assert "to bool" in str(err.value)
502+
503+
def _args_spec() -> tuple:
504+
return ()
505+
506+
chain_var = Var.create(
507+
EventChain(
508+
events=[S.s()],
509+
args_spec=_args_spec,
510+
)
511+
)
512+
with pytest.raises(TypeError) as err:
513+
rx_cond(chain_var, rx.text("True"), rx.text("False"))
514+
assert "Cannot convert" in str(err.value)
515+
assert "to bool" in str(err.value)
516+
517+
488518
def test_decentralized_event_with_args():
489519
"""Test the decentralized event."""
490520

0 commit comments

Comments
 (0)