allow functools partial with foreach (#4709)

This commit is contained in:
Khaleel Al-Adhami 2025-01-28 23:03:22 -08:00 committed by GitHub
parent 58e63f387f
commit 1e8e82ec0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
from __future__ import annotations
import functools
import inspect
from typing import Any, Callable, Iterable
@ -97,9 +98,20 @@ class Foreach(Component):
# Determine the index var name based on the params accepted by render_fn.
props["index_var_name"] = params[1].name
else:
render_fn = self.render_fn
# Otherwise, use a deterministic index, based on the render function bytecode.
code_hash = (
hash(self.render_fn.__code__)
hash(
getattr(
render_fn,
"__code__",
(
repr(self.render_fn)
if not isinstance(render_fn, functools.partial)
else render_fn.func.__code__
),
)
)
.to_bytes(
length=8,
byteorder="big",