Skip to content

Pass a trigger's arguments only to a callable's positional parameters - #7961

Open
FarhanAliRaza wants to merge 3 commits into
mainfrom
farhan/focused-darwin-dx76ek-varargs
Open

FarhanAliRaza wants to merge 3 commits into
mainfrom
farhan/focused-darwin-dx76ek-varargs

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 (#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

A callable given to an event trigger, such as `lambda key, **kw: ...` or
`lambda key, *, flag=1: ...`, raised TypeError because every parameter,
keyword-only and `**kwargs` included, was counted as a slot for the
trigger's arguments. `*args` now takes all of them, even none.

Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner November 22, 2026 02:09
@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR passes trigger arguments only to a callable’s positional parameters.

  • Event callbacks receive trigger values only through positional parameters.
  • Required keyword-only handler values still need a trigger argument.

No new actionable issues were found. Tests were inspected but not run.

Reviews (3) · Last reviewed commit: "Reject a callable whose keyword-only par..." · Reviewed by Greptile

Counting only positional parameters let `def handle(self, value, *,
required)` bind to a one-argument trigger and fail later in the backend.
Only `*args` and `**kwargs` are left out of the count now.

Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

A function given to an event trigger gets the trigger's arguments by
position, so a required keyword-only parameter can never be filled. It
failed with a TypeError when the event fired; it now fails when the page
is built, with an error that names the parameter and the fix.

Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
@codspeed

codspeed Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 5.65%

3 warnings

⚠️ 13 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 4 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 4 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

❌ 4 regressed benchmarks
✅ 142 untouched benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_route_matching[1000] 90 ms 96.4 ms -6.6%
❌ test_route_matching[100] 9.4 ms 10 ms -6.56%
❌ test_route_matching[10] 1.2 ms 1.3 ms -5.21%
❌ test_state_access[write_own] 122.2 ms 127.5 ms -4.22%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing farhan/focused-darwin-dx76ek-varargs (f475bf2) with main (df7f157)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Copy link
Copy Markdown
Contributor Author

CodSpeed: the 4 flagged benchmarks don't run this PR's code.

CodSpeed flags test_route_matching[10|100|1000] and test_state_access[write_own] on f475bf2 (on a6c57f4 it was route matching and test_proxy_list_mutation). This PR changes only call_event_fn, check_fn_match_arg_spec and two new helpers. They run when an event trigger is bound to a callable while the page is built.

I counted calls to those four functions with a sys.setprofile hook during each flagged benchmark:

test_route_matching[10]       {}
test_route_matching[100]      {}
test_route_matching[1000]     {}
test_state_access[write_own]  {}
test_proxy_list_mutation      {}
# control: test_call_event_fn_passes_every_trigger_argument_to_varargs
#   {'call_event_fn': 2, 'check_fn_match_arg_spec': 2, '_max_positional_args': 2, '_check_no_required_keyword_only': 2}

None of the flagged benchmarks makes a single call into the changed code. CodSpeed's own warnings say these 4 benchmarks "ran on different hardware" and "in a different software environment" than their baseline. The first commit (81d3b35), which carried most of the change, reported no performance change. So this looks like runner variance. It can be acknowledged on CodSpeed; there's no code to fix here.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant