Skip to content

Commit 647315d

Browse files
committed
Fix Python 3.7 test
1 parent 6b4cc83 commit 647315d

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

python/ipywidgets/ipywidgets/widgets/tests/utils.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
from ipywidgets import Widget
55
import ipywidgets.widgets.widget
66

7-
import comm
8-
from ipykernel.comm import Comm
7+
# The new comm package is not available in our Python 3.7 CI (older ipykernel version)
8+
try:
9+
import comm
10+
NEW_COMM_PACKAGE = True
11+
except ImportError:
12+
NEW_COMM_PACKAGE = False
13+
14+
import ipykernel.comm
915

1016

1117
class DummyComm():
1218
comm_id = 'a-b-c-d'
1319
kernel = 'Truthy'
1420

1521
def __init__(self, *args, **kwargs):
16-
super().__init__(*args, **kwargs)
22+
super().__init__()
1723
self.messages = []
1824

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

43-
orig_create_comm = comm.create_comm
44-
orig_get_comm_manager = comm.get_comm_manager
49+
if NEW_COMM_PACKAGE:
50+
orig_comm = ipykernel.comm.comm.BaseComm
51+
else:
52+
orig_comm = ipykernel.comm.Comm
53+
orig_create_comm = None
54+
orig_get_comm_manager = None
55+
56+
if NEW_COMM_PACKAGE:
57+
orig_create_comm = comm.create_comm
58+
orig_get_comm_manager = comm.get_comm_manager
4559

4660
def setup_test_comm():
47-
comm.create_comm = dummy_create_comm
48-
comm.get_comm_manager = dummy_get_comm_manager
61+
if NEW_COMM_PACKAGE:
62+
comm.create_comm = dummy_create_comm
63+
comm.get_comm_manager = dummy_get_comm_manager
64+
ipykernel.comm.comm.BaseComm = DummyComm
65+
else:
66+
ipykernel.comm.Comm = DummyComm
4967
Widget.comm.klass = DummyComm
5068
ipywidgets.widgets.widget.Comm = DummyComm
5169
_widget_attrs['_repr_mimebundle_'] = Widget._repr_mimebundle_
@@ -54,10 +72,14 @@ def raise_not_implemented(*args, **kwargs):
5472
Widget._repr_mimebundle_ = raise_not_implemented
5573

5674
def teardown_test_comm():
57-
comm.create_comm = orig_create_comm
58-
comm.get_comm_manager = orig_get_comm_manager
59-
Widget.comm.klass = Comm
60-
ipywidgets.widgets.widget.Comm = Comm
75+
if NEW_COMM_PACKAGE:
76+
comm.create_comm = orig_create_comm
77+
comm.get_comm_manager = orig_get_comm_manager
78+
ipykernel.comm.comm.BaseComm = orig_comm
79+
else:
80+
ipykernel.comm.Comm = orig_comm
81+
Widget.comm.klass = orig_comm
82+
ipywidgets.widgets.widget.Comm = orig_comm
6183
for attr, value in _widget_attrs.items():
6284
if value is undefined:
6385
delattr(Widget, attr)

python/ipywidgets/ipywidgets/widgets/widget.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections.abc import Iterable
1212
from IPython import get_ipython
1313
from traitlets import (
14-
Any, HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
14+
HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
1515
Undefined)
1616
from json import loads as jsonloads, dumps as jsondumps
1717

@@ -479,7 +479,7 @@ def get_view_spec(self):
479479

480480
_view_count = Int(None, allow_none=True,
481481
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)
482-
comm = Any(None, allow_none=True)
482+
comm = Instance(object, allow_none=True)
483483

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

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

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

827827
def _repr_keys(self):

0 commit comments

Comments
 (0)