From e0a7b75405efcec6706e447373d88d8fcb040037 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Tue, 22 Nov 2022 16:06:33 +0100 Subject: [PATCH 1/3] Fix: Inherit from traitlets.HasTraits --- comm/base_comm.py | 11 +++++++---- tests/test_comm.py | 14 +++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/comm/base_comm.py b/comm/base_comm.py index fb92408..fb60b8d 100644 --- a/comm/base_comm.py +++ b/comm/base_comm.py @@ -7,13 +7,14 @@ import uuid import logging +from traitlets import HasTraits from traitlets.utils.importstring import import_item logger = logging.getLogger('Comm') -class BaseComm: +class BaseComm(HasTraits): """Class for communicating between a Frontend and a Kernel Must be subclassed with a publish_msg method implementation which @@ -92,7 +93,6 @@ def open(self, data=None, metadata=None, buffers=None): def close(self, data=None, metadata=None, buffers=None, deleting=False): """Close the frontend-side version of this comm""" - from comm import get_comm_manager if self._closed: # only close once return @@ -107,6 +107,7 @@ def close(self, data=None, metadata=None, buffers=None, deleting=False): ) if not deleting: # If deleting, the comm can't be registered + from comm import get_comm_manager get_comm_manager().unregister_comm(self) def send(self, data=None, metadata=None, buffers=None): @@ -160,15 +161,17 @@ def handle_msg(self, msg): shell.events.trigger("post_execute") -class CommManager: +class CommManager(HasTraits): """Default CommManager singleton implementation for Comms in the Kernel""" # Public APIs - def __init__(self): + def __init__(self, *args, **kwargs): self.comms = {} self.targets = {} + super(CommManager, self).__init__(*args, **kwargs) + def register_target(self, target_name, f): """Register a callable f for a given target name diff --git a/tests/test_comm.py b/tests/test_comm.py index 9e8f8e1..32f2840 100644 --- a/tests/test_comm.py +++ b/tests/test_comm.py @@ -1,3 +1,5 @@ +from traitlets import HasTraits, Instance, Any + from comm.base_comm import CommManager, BaseComm @@ -7,6 +9,11 @@ def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): pass +class CustomCommManager(CommManager): + + parent = Any() + + def test_comm_manager(): test = CommManager() assert test.targets == {} @@ -14,4 +21,9 @@ def test_comm_manager(): def test_base_comm(): test = MyComm() - assert test.target_name == "comm" \ No newline at end of file + assert test.target_name == "comm" + + +def test_custom_comm_manager(): + test = CustomCommManager(parent=3) + assert test.parent == 3 From d78dddda3db6d1fe70e1bd85ea73dcd3d1ada25b Mon Sep 17 00:00:00 2001 From: martinRenou Date: Wed, 23 Nov 2022 09:50:44 +0100 Subject: [PATCH 2/3] Fix: we were not running the tests --- .github/workflows/python-tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 8567aa0..ab177ce 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -30,6 +30,6 @@ jobs: - name: Base Setup uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 - name: Install and test - run: + run: | pip install -e .[test] - pytest + pytest . From 6a071950f370462526596e96de78107b56de21f5 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Wed, 23 Nov 2022 11:56:54 +0100 Subject: [PATCH 3/3] Fix: Inherit from LoggingConfigurable: --- comm/base_comm.py | 6 +++--- tests/test_comm.py | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/comm/base_comm.py b/comm/base_comm.py index fb60b8d..30b2125 100644 --- a/comm/base_comm.py +++ b/comm/base_comm.py @@ -7,14 +7,14 @@ import uuid import logging -from traitlets import HasTraits from traitlets.utils.importstring import import_item +from traitlets.config import LoggingConfigurable logger = logging.getLogger('Comm') -class BaseComm(HasTraits): +class BaseComm(LoggingConfigurable): """Class for communicating between a Frontend and a Kernel Must be subclassed with a publish_msg method implementation which @@ -161,7 +161,7 @@ def handle_msg(self, msg): shell.events.trigger("post_execute") -class CommManager(HasTraits): +class CommManager(LoggingConfigurable): """Default CommManager singleton implementation for Comms in the Kernel""" # Public APIs diff --git a/tests/test_comm.py b/tests/test_comm.py index 32f2840..a297f80 100644 --- a/tests/test_comm.py +++ b/tests/test_comm.py @@ -1,4 +1,5 @@ -from traitlets import HasTraits, Instance, Any +from traitlets import Any +from traitlets.config import LoggingConfigurable from comm.base_comm import CommManager, BaseComm @@ -25,5 +26,5 @@ def test_base_comm(): def test_custom_comm_manager(): - test = CustomCommManager(parent=3) - assert test.parent == 3 + test = CustomCommManager(parent=LoggingConfigurable()) + assert test.parent is not None