Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rx.foreach crashes on hashing the render function's code when the render function is a functools.partial #4625

Open
gmos opened this issue Jan 12, 2025 · 1 comment

Comments

@gmos
Copy link

gmos commented Jan 12, 2025

Describe the bug
rx.foreach() crashes on hashing the render function's code when the render function is a functools.partial function.

To Reproduce
Steps to reproduce the behavior:

_display_primary_color = partial(_display_color, "accent_color")

def primary_color_picker() -> rx.Component:
    return rx.flex(
        rx.foreach(ColorPickerState.primary_color_options, _display_primary_color),
        **PICKER_PARS,
    )

Expected behavior
Expected the partial function to just work.

Screenshots
N/A

Specifics (please complete the following information):

  • Python Version: 3.13
  • Reflex Version: 0.6.7
  • OS: Ubuntu 24.04
  • Browser (Optional): Chrome

Additional context
In method _render of class Foreach in file foreach.py the props["index_var_name"] can be derived from a hash of the render function's byte code. However, functools.partial() functions do not have bytecode.

Suggested fix
The following code fixes the issue. Since the bytecode will be the same for all partial functions derived from the same base function, the 'frozen' parameters are made part of the hash by using the repr.

            ...
            code_hash = (
                hash(
                    getattr(self.render_fn, "__code__", None) or
                    # Fall back to the repr of the function if the code object is not available.
                    # This will handle functools.partial() functions nicely, including the fixed paramater values.
                    repr(self.render_fn)
                )
                .to_bytes(
                    length=8,
                    byteorder="big",
                    signed=True,
                )
                .hex()
            )
            ...
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

No branches or pull requests

1 participant