From eddfdc64757ab65112bf510c2842716ab6b032e3 Mon Sep 17 00:00:00 2001 From: Xiao Chen Date: Fri, 10 Jun 2022 15:02:05 -0700 Subject: [PATCH] Chekc self.keys before trait_metadata in set_state --- python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py | 4 ++-- python/ipywidgets/ipywidgets/widgets/widget.py | 4 ++-- python/jupyterlab_widgets/src/manager.ts | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py b/python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py index 6d8a8abebb..bee5cac16a 100644 --- a/python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py +++ b/python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py @@ -304,8 +304,8 @@ class ValueWidget(Widget): assert widget.value == 1 widget._send = mock.MagicMock() - # this mimics a value coming from the front end - widget.set_state({'value': 42}) + # this mimics a state coming from the front end + widget.set_state({'value': 42, 'unexpected_field': 43}) assert widget.value == 42 # we expect this to be echoed diff --git a/python/ipywidgets/ipywidgets/widgets/widget.py b/python/ipywidgets/ipywidgets/widgets/widget.py index a57d45a8da..703ac64a6d 100644 --- a/python/ipywidgets/ipywidgets/widgets/widget.py +++ b/python/ipywidgets/ipywidgets/widgets/widget.py @@ -566,8 +566,8 @@ def set_state(self, sync_data): # Send an echo update message immediately if JUPYTER_WIDGETS_ECHO: echo_state = {} - for attr,value in sync_data.items(): - if self.trait_metadata(attr, 'echo_update', default=True): + for attr, value in sync_data.items(): + if attr in self.keys and self.trait_metadata(attr, 'echo_update', default=True): echo_state[attr] = value if echo_state: echo_state, echo_buffer_paths, echo_buffers = _remove_buffers(echo_state) diff --git a/python/jupyterlab_widgets/src/manager.ts b/python/jupyterlab_widgets/src/manager.ts index 5432fe951e..e98bd4975c 100644 --- a/python/jupyterlab_widgets/src/manager.ts +++ b/python/jupyterlab_widgets/src/manager.ts @@ -421,6 +421,7 @@ export class WidgetManager extends LabWidgetManager { this._context = context; context.sessionContext.kernelChanged.connect((sender, args) => { + console.log('kernel changed'); this._handleKernelChanged(args); });