Skip to content

fix: accept bound methods as rx.foreach render functions - #7957

Open
benedikt-bartscher wants to merge 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix-foreach-bound-method-render-fn
Open

benedikt-bartscher wants to merge 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix-foreach-bound-method-render-fn

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

rx.foreach rejects a bound method as its render function:

class Page:
    @classmethod
    def row(cls, item: rx.Var[str]) -> rx.Component:
        return rx.text(item)

    @classmethod
    def row_with_index(cls, item: rx.Var[str], index: rx.Var[int]) -> rx.Component:
        return rx.text(item, index)


rx.foreach(State.items, Page.row)             # TypeError: row() takes 2 positional arguments but 3 were given
rx.foreach(State.items, Page.row_with_index)  # ValueError: The render function must take 2 arguments.

Foreach._render counts the render function's parameters with inspect.signature, which leaves out a bound method's cls/self. IterTag.render_component counted them again with inspect.getfullargspec(...).args, which includes it. So a one-argument classmethod was called with the index as well, and a two-argument classmethod or instance method failed the "must take 2 arguments" check. Both now count the render function's parameters with the same helper, _render_fn_params: only positional parameters take the item and the index, so keyword-only parameters and **kwargs never receive the index. A render function without positional parameters, like rx.text (Text.create(cls, *children, **props)), gets the item only, as it did before.

The workaround is a lambda (lambda item: Page.row(item)), which ruff's PLW0108 (unnecessary-lambda) then flags.

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches the shared `rx.foreach` callback behavior.

Raise the minimum version of reflex-base before merging so allowed installations can import foreach.py.

Findings

  1. P1 Allowed installations fail to import ▶

Summary

rx.foreach accepts bound methods and uses one helper to decide which parameters receive the item and index.

  • rx.foreach passes bound methods only their item and optional index.

Reviews (2) · Last reviewed commit: "Merge commit 'a96668a385f626ce80b5d2b5e0..." · Reviewed by Greptile

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 21, 2026 19:39
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 21, 2026 19:39
@codspeed

codspeed Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 5.6%

2 warnings

⚠️ 19 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

⚠️ 1 benchmark with a significant change ran in a different software environment

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

See affected benchmarks

❌ 1 regressed benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_state_access[write_own] 122.2 ms 129.4 ms -5.6%

Tip

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


Comparing benedikt-bartscher:fix-foreach-bound-method-render-fn (fe0c2d8) 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. ↩

@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 | Re-trigger cubic

…ach-bound-method-render-fn

# Conflicts:
#	packages/reflex-base/src/reflex_base/components/tags/iter_tag.py

from reflex_base.components.component import Component, field
from reflex_base.components.tags import IterTag
from reflex_base.components.tags.iter_tag import _render_fn_params

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.

P1 Allowed installations fail to import

foreach.py now imports _render_fn_params, but reflex-components-core still allows reflex-base >= 0.11.0. That released version lacks the helper, so an allowed installation fails with ImportError when importing foreach.py.

The repository guide requires raising a sibling dependency's minimum version when using its unreleased code. Raise the reflex-base minimum in packages/reflex-components-core/pyproject.toml to 0.11.1.dev0.

Context Used: CLAUDE.md (source)

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