Skip to content

Commit c43c655

Browse files
committed
Do not load comm registry entries at startup
1 parent b442df6 commit c43c655

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

euporie/core/comm/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
"""Sub-module for handling kernel Comm messages."""
2-
3-
from . import ipympl as ipympl

euporie/core/comm/ipywidgets.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,10 @@ def open_comm_ipywidgets(
14571457
The initialized widget Comm object.
14581458
"""
14591459
model_name = data.get("state", {}).get("_model_name")
1460-
# Skip type checking due to https://github.com/python/mypy/issues/3115
14611460
return WIDGET_MODELS.get(model_name, UnimplementedModel)(
14621461
comm_container, comm_id, data, buffers
1463-
) # type: ignore
1462+
)
1463+
1464+
1465+
# Load ``ipympl`` widget
1466+
from . import ipympl as ipympl # noqa: E402

euporie/core/comm/registry.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
from __future__ import annotations
44

5+
from pkgutil import resolve_name
56
from typing import TYPE_CHECKING
67

78
from euporie.core.comm.base import UnimplementedComm
8-
from euporie.core.comm.ipywidgets import open_comm_ipywidgets
99

1010
if TYPE_CHECKING:
1111
from collections.abc import Sequence
1212
from typing import Any, Callable
1313

1414
from euporie.core.comm.base import Comm, KernelTab
1515

16-
TARGET_CLASSES: dict[str, Callable[[KernelTab, str, dict, Sequence[bytes]], Comm]] = {
17-
"jupyter.widget": open_comm_ipywidgets
16+
TARGET_CLASSES: dict[str, str] = {
17+
"jupyter.widget": "euporie.core.comm.ipywidgets:open_comm_ipywidgets"
1818
}
1919

2020

@@ -38,7 +38,13 @@ def open_comm(
3838
3939
"""
4040
target_name = content.get("target_name", "")
41-
return TARGET_CLASSES.get(target_name, UnimplementedComm)(
41+
if path := TARGET_CLASSES.get(target_name):
42+
TargetClass: Callable[[KernelTab, str, dict, Sequence[bytes]], Comm] = (
43+
resolve_name(path)
44+
)
45+
else:
46+
TargetClass = UnimplementedComm
47+
return TargetClass(
4248
# comm_container=
4349
comm_container,
4450
# comm_id=

tests/test_core_comm_registry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ def test_open_comm_ipywidgets() -> None:
2323
assert isinstance(result, IpyWidgetComm)
2424

2525

26+
class TestComm(UnimplementedComm):
27+
pass
28+
29+
2630
def test_open_comm_with_target_class() -> None:
2731
"""`open_comm` returns an instance of the specified target class."""
2832

29-
class TestComm(UnimplementedComm):
30-
pass
31-
32-
TARGET_CLASSES["test.target"] = TestComm
33+
TARGET_CLASSES["test.target"] = "tests.test_core_comm_registry:TestComm"
3334
comm_container = Mock(KernelTab)
3435
content = {
3536
"target_name": "test.target",

0 commit comments

Comments
 (0)