Skip to content

Commit

Permalink
Add validation for ReactiveHTML children (#6941)
Browse files Browse the repository at this point in the history
* Add validation for ReactiveHTML children

* Improve error handling
  • Loading branch information
philippjfr authored Jun 27, 2024
1 parent 96b2da8 commit 70a27c9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions panel/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
)
from .util.checks import import_available
from .viewable import (
Child, Layoutable, Renderable, Viewable,
Child, Children, Layoutable, Renderable, Viewable,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -1716,6 +1716,7 @@ class ReactiveHTML(ReactiveCustomBase, metaclass=ReactiveHTMLMetaclass):

def __init__(self, **params):
from .pane import panel
cls = type(self)
for children_param in self._parser.children.values():
mode = self._child_config.get(children_param, 'model')
if children_param not in params or mode != 'model':
Expand All @@ -1736,7 +1737,23 @@ def __init__(self, **params):
children[key] = panel(pane)
params[children_param] = children
else:
params[children_param] = panel(child_value)
params[children_param] = children = panel(child_value)
child_param = cls.param[children_param]
try:
if children_param not in self._param__private.explicit_no_refs:
children = resolve_value(children)
if not ((isinstance(child_param, Children) and isinstance(children, list)) or
(isinstance(child_param, Child))):
child_param._validate(children)
except Exception as e:
raise RuntimeError(
f"{cls.__name__}._template declares {children_param!r} "
"parameter as a child, however the parameter is of type "
f"{type(child_param).__name__}. Either ensure that the parameter "
"can accept a Panel component, use literal template using the "
"Jinja2 templating syntax (i.e. {{ <param> }}) or declare the "
"child as a literal in the _child_config."
) from e
super().__init__(**params)
self._attrs = {}
self._panes = {}
Expand Down

0 comments on commit 70a27c9

Please sign in to comment.