Skip to content

Commit

Permalink
fix(experimental): Bind self to command
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Apr 19, 2024
1 parent a4b0ec0 commit 833c177
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-walls-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"anywidget": patch
---

Ensure anywidget.experimental.command is called with self
11 changes: 3 additions & 8 deletions anywidget/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,8 @@ def command(cmd: _AnyWidgetCommand) -> _AnyWidgetCommand:
return cmd


_AnyWidgetCommandBound = typing.Callable[
[typing.Any, typing.List[bytes]], typing.Tuple[typing.Any, typing.List[bytes]]
]


def _collect_anywidget_commands(widget_cls: type) -> None:
cmds: dict[str, _AnyWidgetCommandBound] = {}
cmds: dict[str, _AnyWidgetCommand] = {}
for base in widget_cls.__mro__:
if not hasattr(base, "__dict__"):
continue
Expand All @@ -144,7 +139,7 @@ def _register_anywidget_commands(widget: WidgetBase) -> None:
"""Register a custom message reducer for a widget if it implements the protocol."""
# Only add the callback if the widget has any commands.
cmds = typing.cast(
"dict[str, _AnyWidgetCommandBound]",
"dict[str, _AnyWidgetCommand]",
getattr(type(widget), _ANYWIDGET_COMMANDS, {}),
)
if not cmds:
Expand All @@ -156,7 +151,7 @@ def handle_anywidget_command(
if not isinstance(msg, dict) or msg.get("kind") != "anywidget-command":
return
cmd = cmds[msg["name"]]
response, buffers = cmd(msg["msg"], buffers)
response, buffers = cmd(widget, msg["msg"], buffers)
self.send(
{
"id": msg["id"],
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pattern = "\"version\": \"(?P<version>.+?)\""

[tool.hatch.envs.default]
features = ["test", "dev"]
uv = true

[tool.hatch.envs.default.scripts]
lint = [
Expand Down

0 comments on commit 833c177

Please sign in to comment.