Name the child type in ChildrenTypeError and hint how to run event children

Merged
SO
13h ago
+59-4

Related to #5443.

Problem

ChildrenTypeError printed the full repr of the invalid child. For an event such as rx.redirect("/x"), the message was about 950 characters, mostly nested Var reprs. It also did not say how to run an event.

Change

  • The message names the child's type, not its repr:

    ChildrenTypeError: Component Box received child of type EventSpec. Accepted types are other components, state vars, or primitive Python types (dict excluded).
    Bind an event to an event trigger such as on_click, or pass it to on_load in app.add_page to run it when the page loads. See https://reflex.dev/docs/events/events-overview/
    
  • An EventSpec, EventHandler or EventChain child gets the second line as an exception note (add_note). The main message states the problem; the note gives the fix and the docs link.

  • The extra check runs only on the error path. Valid children cost nothing more.

Not in scope

An event inside rx.cond or rx.match still raises no error in Python. Detecting that needed a type check on every Var child and a change to the rx.match result type. The rx.match change broke typed props whose branches mix types, for example rx.progress(value=rx.match(S.page, ("a", 12.5), 50)). Both changes are removed from this PR.

Tests

tests/units/components/test_component.py:

  • test_component_create_event_child: EventSpec, EventHandler and EventChain children give the short message and the note.
  • test_component_create_backend_var_child: updated for the type-name message.