Skip to content

Commit

Permalink
feat(anywidget): Suppress errors when inspecting for RPC commands (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored Apr 2, 2024
1 parent bf933f3 commit 28c4307
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pink-impalas-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"anywidget": patch
---

feat: Suppress errors when inspecting widget for commands
2 changes: 1 addition & 1 deletion anywidget/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _collect_commands(widget: WidgetBase) -> dict[str, _AnyWidgetCommandBound]:
for attr_name in dir(widget):
# suppressing silly assertion erro from ipywidgets _staticproperty
# ref: https://github.com/jupyter-widgets/ipywidgets/blob/b78de43e12ff26e4aa16e6e4c6844a7c82a8ee1c/python/ipywidgets/ipywidgets/widgets/widget.py#L291-L297
with contextlib.suppress(AssertionError):
with contextlib.suppress(Exception):
attr = getattr(widget, attr_name)
if callable(attr) and getattr(attr, _ANYWIDGET_COMMAND, False):
cmds[attr_name] = attr
Expand Down
26 changes: 26 additions & 0 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,29 @@ def _echo2(

w = Widget()
assert len(w._msg_callbacks.callbacks) == 1


def test_supresses_error_in_constructor():
import anywidget.experimental

class Widget(anywidget.AnyWidget):
_esm = """
export default {
async render({ model, el, experimental }) {
let [msg] = await experimental.invoke("_echo", "hi");
}
}
"""

@anywidget.experimental.command
def _echo(
self, msg: typing.Any, buffers: list[bytes]
) -> tuple[str, list[bytes]]:
return msg, buffers

@property
def value(self):
raise ValueError("error")

w = Widget()
assert len(w._msg_callbacks.callbacks) == 1

0 comments on commit 28c4307

Please sign in to comment.