From 2d4bed2fd441edd315612087793f007e995ed897 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 16 Feb 2023 16:38:12 +0100 Subject: [PATCH 1/9] Deprecate Comm class --- ipykernel/comm/comm.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ipykernel/comm/comm.py b/ipykernel/comm/comm.py index e236bee7..d9a36ba5 100644 --- a/ipykernel/comm/comm.py +++ b/ipykernel/comm/comm.py @@ -3,8 +3,9 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -import uuid from typing import Optional +import uuid +from warnings import warn import comm.base_comm import traitlets.config @@ -72,6 +73,12 @@ def _default_comm_id(self): def __init__(self, target_name='', data=None, metadata=None, buffers=None, **kwargs): """Initialize a comm.""" + warn( + "The `ipykernel.comm.Comm` class has been deprecated. Please use the `comm` module instead." + "For creating comms, use the function `from comm import create_comm`.", + DeprecationWarning, + ) + # Handle differing arguments between base classes. had_kernel = 'kernel' in kwargs kernel = kwargs.pop('kernel', None) From 4496380c93a430b717614ee44285249642e5a693 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Feb 2023 15:39:12 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/comm/comm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipykernel/comm/comm.py b/ipykernel/comm/comm.py index d9a36ba5..38cb6438 100644 --- a/ipykernel/comm/comm.py +++ b/ipykernel/comm/comm.py @@ -3,8 +3,8 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from typing import Optional import uuid +from typing import Optional from warnings import warn import comm.base_comm From 41137e56249effefab00853455be09259ad95c43 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 16 Feb 2023 16:47:40 +0100 Subject: [PATCH 3/9] Fix tests --- ipykernel/tests/test_comm.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ipykernel/tests/test_comm.py b/ipykernel/tests/test_comm.py index 5ec0d455..86ec1228 100644 --- a/ipykernel/tests/test_comm.py +++ b/ipykernel/tests/test_comm.py @@ -1,3 +1,5 @@ +import pytest + import unittest.mock from ipykernel.comm import Comm, CommManager @@ -9,7 +11,8 @@ def test_comm(kernel: Kernel) -> None: manager = CommManager(kernel=kernel) kernel.comm_manager = manager # type:ignore - c = Comm(kernel=kernel, target_name="bar") + with pytest.deprecated_call(): + c = Comm(kernel=kernel, target_name="bar") msgs = [] assert kernel is c.kernel # type:ignore @@ -53,7 +56,8 @@ def on_msg(msg): kernel.comm_manager = manager # type:ignore with unittest.mock.patch.object(Comm, "publish_msg") as publish_msg: - comm = Comm() + with pytest.deprecated_call(): + comm = Comm() comm.on_msg(on_msg) comm.on_close(on_close) manager.register_comm(comm) @@ -96,5 +100,7 @@ def on_msg(msg): def test_comm_in_manager(ipkernel: IPythonKernel) -> None: - comm = Comm() + with pytest.deprecated_call(): + comm = Comm() + assert comm.comm_id in ipkernel.comm_manager.comms From 20cf16ee2a6df614b733608871fbd12f17a3ecfa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Feb 2023 15:54:29 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/tests/test_comm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ipykernel/tests/test_comm.py b/ipykernel/tests/test_comm.py index 86ec1228..728731ce 100644 --- a/ipykernel/tests/test_comm.py +++ b/ipykernel/tests/test_comm.py @@ -1,7 +1,7 @@ -import pytest - import unittest.mock +import pytest + from ipykernel.comm import Comm, CommManager from ipykernel.ipkernel import IPythonKernel from ipykernel.kernelbase import Kernel From dde4fa2ea4700ecc06355f57ef0a287c13710e78 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 16 Mar 2023 15:19:30 +0100 Subject: [PATCH 5/9] Fix comm creation --- ipykernel/comm/comm.py | 13 +++++++------ ipykernel/comm/manager.py | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/ipykernel/comm/comm.py b/ipykernel/comm/comm.py index 38cb6438..964d517e 100644 --- a/ipykernel/comm/comm.py +++ b/ipykernel/comm/comm.py @@ -71,13 +71,14 @@ def _default_kernel(self): def _default_comm_id(self): return uuid.uuid4().hex - def __init__(self, target_name='', data=None, metadata=None, buffers=None, **kwargs): + def __init__(self, target_name='', data=None, metadata=None, buffers=None, show_warning=True, **kwargs): """Initialize a comm.""" - warn( - "The `ipykernel.comm.Comm` class has been deprecated. Please use the `comm` module instead." - "For creating comms, use the function `from comm import create_comm`.", - DeprecationWarning, - ) + if show_warning: + warn( + "The `ipykernel.comm.Comm` class has been deprecated. Please use the `comm` module instead." + "For creating comms, use the function `from comm import create_comm`.", + DeprecationWarning, + ) # Handle differing arguments between base classes. had_kernel = 'kernel' in kwargs diff --git a/ipykernel/comm/manager.py b/ipykernel/comm/manager.py index f18e272d..2ded953c 100644 --- a/ipykernel/comm/manager.py +++ b/ipykernel/comm/manager.py @@ -3,11 +3,17 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. +import logging import comm.base_comm + import traitlets import traitlets.config +from .comm import Comm + +logger = logging.getLogger("ipykernel.comm") + class CommManager(comm.base_comm.CommManager, traitlets.config.LoggingConfigurable): """A comm manager.""" @@ -21,3 +27,37 @@ def __init__(self, **kwargs): # CommManager doesn't take arguments, so we explicitly forward arguments comm.base_comm.CommManager.__init__(self) traitlets.config.LoggingConfigurable.__init__(self, **kwargs) + + def comm_open(self, stream, ident, msg): + """Handler for comm_open messages""" + # This is for backward compatibility, the comm_open creates a a new ipykernel.comm.Comm + # but we should let the base class create the comm with comm.create_comm in a major release + content = msg["content"] + comm_id = content["comm_id"] + target_name = content["target_name"] + f = self.targets.get(target_name, None) + comm = Comm( + comm_id=comm_id, + primary=False, + target_name=target_name, + show_warning=False, + ) + self.register_comm(comm) + if f is None: + logger.error("No such comm target registered: %s", target_name) + else: + try: + f(comm, msg) + return + except Exception: + logger.error("Exception opening comm with target: %s", target_name, exc_info=True) + + # Failure. + try: + comm.close() + except Exception: + logger.error( + """Could not close comm during `comm_open` failure + clean-up. The comm may not have been opened yet.""", + exc_info=True, + ) From d3ca2e9b309b65182f89dc41c4ede530b5270f37 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Mar 2023 14:19:53 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/comm/comm.py | 4 +++- ipykernel/comm/manager.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ipykernel/comm/comm.py b/ipykernel/comm/comm.py index 964d517e..01cc4d7b 100644 --- a/ipykernel/comm/comm.py +++ b/ipykernel/comm/comm.py @@ -71,7 +71,9 @@ def _default_kernel(self): def _default_comm_id(self): return uuid.uuid4().hex - def __init__(self, target_name='', data=None, metadata=None, buffers=None, show_warning=True, **kwargs): + def __init__( + self, target_name='', data=None, metadata=None, buffers=None, show_warning=True, **kwargs + ): """Initialize a comm.""" if show_warning: warn( diff --git a/ipykernel/comm/manager.py b/ipykernel/comm/manager.py index 2ded953c..7b0de693 100644 --- a/ipykernel/comm/manager.py +++ b/ipykernel/comm/manager.py @@ -6,7 +6,6 @@ import logging import comm.base_comm - import traitlets import traitlets.config From 18200abd67b0453477595bfb1d572d3b92bc7a95 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 16 Mar 2023 15:52:23 +0100 Subject: [PATCH 7/9] Don't fail on warnings mismatch for ipywidgets downstream tests --- .github/workflows/downstream.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/downstream.yml b/.github/workflows/downstream.yml index a37242e6..a3c544e2 100644 --- a/.github/workflows/downstream.yml +++ b/.github/workflows/downstream.yml @@ -38,6 +38,7 @@ jobs: uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1 with: package_name: ipywidgets + test_command: pytest -vv -raXxs -k "not deprecation_fa_icons" -W default --durations 10 --color=yes jupyter_client: runs-on: ubuntu-latest From adbdcc51faa4f84bf1a75c90a5d9a4e3176aa9eb Mon Sep 17 00:00:00 2001 From: martinRenou Date: Fri, 17 Mar 2023 09:38:47 +0100 Subject: [PATCH 8/9] Escape --- .github/workflows/downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/downstream.yml b/.github/workflows/downstream.yml index a3c544e2..8a33a1ae 100644 --- a/.github/workflows/downstream.yml +++ b/.github/workflows/downstream.yml @@ -38,7 +38,7 @@ jobs: uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1 with: package_name: ipywidgets - test_command: pytest -vv -raXxs -k "not deprecation_fa_icons" -W default --durations 10 --color=yes + test_command: pytest -vv -raXxs -k \"not deprecation_fa_icons\" -W default --durations 10 --color=yes jupyter_client: runs-on: ubuntu-latest From f35fa8a742174612ce5f4f8da44a43a3c10e33f4 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Fri, 17 Mar 2023 09:41:20 +0100 Subject: [PATCH 9/9] Disable more deprecation tests in ipywidgets tests --- .github/workflows/downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/downstream.yml b/.github/workflows/downstream.yml index 8a33a1ae..f4ee5cf9 100644 --- a/.github/workflows/downstream.yml +++ b/.github/workflows/downstream.yml @@ -38,7 +38,7 @@ jobs: uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1 with: package_name: ipywidgets - test_command: pytest -vv -raXxs -k \"not deprecation_fa_icons\" -W default --durations 10 --color=yes + test_command: pytest -vv -raXxs -k \"not deprecation_fa_icons and not tooltip_deprecation and not on_submit_deprecation\" -W default --durations 10 --color=yes jupyter_client: runs-on: ubuntu-latest