Skip to content

Commit 1da941b

Browse files
committed
component key only given if requested
that is, if `key` is a kwarg of the function, it will be given as None when no key was specified, and the given value otherwise.
1 parent b7e7292 commit 1da941b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/idom/core/component.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ def component(function: ComponentRenderFunction) -> Callable[..., "Component"]:
2626
Parameters:
2727
function: The function that will render a :class:`VdomDict`.
2828
"""
29+
sig = inspect.signature(function)
30+
key_is_kwarg = "key" in sig.parameters and sig.parameters["key"].kind in (
31+
inspect.Parameter.KEYWORD_ONLY,
32+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
33+
)
2934

3035
@wraps(function)
3136
def constructor(*args: Any, key: Optional[Any] = None, **kwargs: Any) -> Component:
37+
if key_is_kwarg:
38+
kwargs["key"] = key
3239
return Component(function, key, args, kwargs)
3340

3441
return constructor
@@ -63,8 +70,6 @@ def __init__(
6370
self._kwargs = kwargs
6471
self.id = uuid4().hex
6572
self.key = key
66-
if key is not None:
67-
kwargs["key"] = key
6873

6974
def render(self) -> VdomDict:
7075
model = self._func(*self._args, **self._kwargs)

0 commit comments

Comments
 (0)