Skip to content

Commit

Permalink
Fix Python 3.7 test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Feb 16, 2023
1 parent 6b4cc83 commit 647315d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
44 changes: 33 additions & 11 deletions python/ipywidgets/ipywidgets/widgets/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
from ipywidgets import Widget
import ipywidgets.widgets.widget

import comm
from ipykernel.comm import Comm
# The new comm package is not available in our Python 3.7 CI (older ipykernel version)
try:
import comm
NEW_COMM_PACKAGE = True
except ImportError:
NEW_COMM_PACKAGE = False

import ipykernel.comm


class DummyComm():
comm_id = 'a-b-c-d'
kernel = 'Truthy'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
super().__init__()
self.messages = []

def open(self, *args, **kwargs):
Expand All @@ -40,12 +46,24 @@ def dummy_get_comm_manager(**kwargs):
_widget_attrs = {}
undefined = object()

orig_create_comm = comm.create_comm
orig_get_comm_manager = comm.get_comm_manager
if NEW_COMM_PACKAGE:
orig_comm = ipykernel.comm.comm.BaseComm
else:
orig_comm = ipykernel.comm.Comm
orig_create_comm = None
orig_get_comm_manager = None

if NEW_COMM_PACKAGE:
orig_create_comm = comm.create_comm
orig_get_comm_manager = comm.get_comm_manager

def setup_test_comm():
comm.create_comm = dummy_create_comm
comm.get_comm_manager = dummy_get_comm_manager
if NEW_COMM_PACKAGE:
comm.create_comm = dummy_create_comm
comm.get_comm_manager = dummy_get_comm_manager
ipykernel.comm.comm.BaseComm = DummyComm
else:
ipykernel.comm.Comm = DummyComm
Widget.comm.klass = DummyComm
ipywidgets.widgets.widget.Comm = DummyComm
_widget_attrs['_repr_mimebundle_'] = Widget._repr_mimebundle_
Expand All @@ -54,10 +72,14 @@ def raise_not_implemented(*args, **kwargs):
Widget._repr_mimebundle_ = raise_not_implemented

def teardown_test_comm():
comm.create_comm = orig_create_comm
comm.get_comm_manager = orig_get_comm_manager
Widget.comm.klass = Comm
ipywidgets.widgets.widget.Comm = Comm
if NEW_COMM_PACKAGE:
comm.create_comm = orig_create_comm
comm.get_comm_manager = orig_get_comm_manager
ipykernel.comm.comm.BaseComm = orig_comm
else:
ipykernel.comm.Comm = orig_comm
Widget.comm.klass = orig_comm
ipywidgets.widgets.widget.Comm = orig_comm
for attr, value in _widget_attrs.items():
if value is undefined:
delattr(Widget, attr)
Expand Down
8 changes: 4 additions & 4 deletions python/ipywidgets/ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections.abc import Iterable
from IPython import get_ipython
from traitlets import (
Any, HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
Undefined)
from json import loads as jsonloads, dumps as jsondumps

Expand Down Expand Up @@ -479,7 +479,7 @@ def get_view_spec(self):

_view_count = Int(None, allow_none=True,
help="EXPERIMENTAL: The number of views of the model displayed in the frontend. This attribute is experimental and may change or be removed in the future. None signifies that views will not be tracked. Set this to 0 to start tracking view creation/deletion.").tag(sync=True)
comm = Any(None, allow_none=True)
comm = Instance(object, allow_none=True)

keys = List(help="The traits which are synced.")

Expand Down Expand Up @@ -693,7 +693,7 @@ def notify_change(self, change):
# Send the state to the frontend before the user-registered callbacks
# are called.
name = change['name']
if self.comm is not None:
if self.comm is not None and (self.comm.kernel is not None if hasattr(self.comm, "kernel") else True):
# Make sure this isn't information that the front-end just sent us.
if name in self.keys and self._should_send_property(name, getattr(self, name)):
# Send new state to front-end
Expand Down Expand Up @@ -821,7 +821,7 @@ def _repr_mimebundle_(self, **kwargs):

def _send(self, msg, buffers=None):
"""Sends a message to the model in the front-end."""
if self.comm is not None:
if self.comm is not None and (self.comm.kernel is not None if hasattr(self.comm, "kernel") else True):
self.comm.send(data=msg, buffers=buffers)

def _repr_keys(self):
Expand Down

0 comments on commit 647315d

Please sign in to comment.