Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Annotated,
Any,
Generic,
NoReturn,
Protocol,
TypedDict,
TypeVar,
Expand Down Expand Up @@ -1683,6 +1684,16 @@ def get_fn_signature(fn: Callable) -> inspect.Signature:
class EventVar(ObjectVar, python_types=(EventSpec, EventHandler)):
"""Base class for event vars."""

def bool(self) -> NoReturn:
"""Get the boolean value of the var.

Raises:
TypeError: EventVar cannot be converted to a boolean.
"""
raise TypeError(
f"Cannot convert {self._js_expr} of type {type(self).__name__} to bool."
)


@dataclasses.dataclass(
eq=False,
Expand Down Expand Up @@ -1759,6 +1770,16 @@ def no_args():
class EventChainVar(BuilderFunctionVar, python_types=EventChain):
"""Base class for event chain vars."""

def bool(self) -> NoReturn:
"""Get the boolean value of the var.

Raises:
TypeError: EventChainVar cannot be converted to a boolean.
"""
raise TypeError(
f"Cannot convert {self._js_expr} of type {type(self).__name__} to bool."
)


@dataclasses.dataclass(
eq=False,
Expand Down
30 changes: 30 additions & 0 deletions tests/units/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,36 @@ def get_handler(self, arg: Var[str]):
_ = rx.input(on_change=w.get_handler)


def test_event_var_in_rx_cond():
"""Test that EventVar and EventChainVar cannot be used in rx.cond()."""
from reflex.components.core.cond import cond as rx_cond

class S(BaseState):
@event
def s(self):
pass

handler_var = Var.create(S.s)
with pytest.raises(TypeError) as err:
rx_cond(handler_var, rx.text("True"), rx.text("False"))
assert "Cannot convert" in str(err.value)
assert "to bool" in str(err.value)

def _args_spec() -> tuple:
return ()

chain_var = Var.create(
EventChain(
events=[S.s()],
args_spec=_args_spec,
)
)
with pytest.raises(TypeError) as err:
rx_cond(chain_var, rx.text("True"), rx.text("False"))
assert "Cannot convert" in str(err.value)
assert "to bool" in str(err.value)


def test_decentralized_event_with_args():
"""Test the decentralized event."""

Expand Down
Loading