Pass a trigger's arguments only to a callable's positional parameters

FA
1h ago
+164-8

Type of change

  • Bug fix (non-breaking change which fixes an issue)

What changed

A callable passed to an event trigger was given the trigger's arguments by counting every parameter of the callable as a slot, including keyword-only parameters and **kwargs. So these raised TypeError:

rx.input(on_key_down=lambda key, **kw: rx.console_log(key))
# TypeError: <lambda>() takes 1 positional argument but 2 were given
rx.input(on_key_down=lambda key, *, flag=1: rx.console_log(key))
  • call_event_fn now passes the trigger's arguments only to the positional parameters (POSITIONAL_ONLY / POSITIONAL_OR_KEYWORD). *args takes all of them, including none, so lambda *args: ... also works on a trigger with no arguments (on_mount).
  • A callable with a keyword-only parameter that has no default can never receive it, because a callable gets the trigger's arguments by position. Such a callable now fails when the page is built, with an EventFnArgMismatchError that names the parameter and the fix (give it a default, or move it before the *). Before, it failed with a raw TypeError when the event fired.
  • check_fn_match_arg_spec no longer counts *args and **kwargs as required arguments. A keyword-only parameter without a default still counts as required, so def handle(self, value, *, required) on a one-argument trigger is still rejected when the page is built. A state handler's arguments are sent by name.

Split out of the JS-modules PR (reflex-dev/reflex#7950), whose imported JS functions take *args. That PR carries this change until this one merges.

Tests

tests/units/reflex_base/event/test_init.py has four new regression tests:

  • *args with 0 and 2 trigger arguments.
  • **kwargs and a keyword-only parameter with a default.
  • A state handler with a required keyword-only parameter.
  • A callable with a required keyword-only parameter on a two-argument trigger.

The tests compare each full error message. Each one fails without the change it covers and passes now.

100% line and branch coverage of the changed lines. Ruff, pyright and C901 (max 8) are clean.

https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf