Pass a trigger's arguments only to a callable's positional parameters
FA
1h ago+164+164+012345678901234567890123456789-8-8-0123456789
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_fnnow passes the trigger's arguments only to the positional parameters (POSITIONAL_ONLY/POSITIONAL_OR_KEYWORD).*argstakes all of them, including none, solambda *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
EventFnArgMismatchErrorthat names the parameter and the fix (give it a default, or move it before the*). Before, it failed with a rawTypeErrorwhen the event fired. check_fn_match_arg_specno longer counts*argsand**kwargsas required arguments. A keyword-only parameter without a default still counts as required, sodef 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:
*argswith 0 and 2 trigger arguments.**kwargsand 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.