fix: accept bound methods as rx.foreach render functions

BE
18m ago
+158-4

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.