Skip to content

Commit 981f93e

Browse files
authored
Merge pull request jupyter-widgets#3484 from xiaochen-db/check-keys-for-echo-update
Check self.keys before accessing trait in set_state()
2 parents d1491e9 + 3f9faea commit 981f93e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

python/ipywidgets/ipywidgets/widgets/tests/test_set_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ class ValueWidget(Widget):
304304
assert widget.value == 1
305305

306306
widget._send = mock.MagicMock()
307-
# this mimics a value coming from the front end
308-
widget.set_state({'value': 42})
307+
# this mimics a state coming from the front end
308+
widget.set_state({'value': 42, 'unexpected_field': 43})
309309
assert widget.value == 42
310310

311311
# we expect this to be echoed

python/ipywidgets/ipywidgets/widgets/widget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ def set_state(self, sync_data):
566566
# Send an echo update message immediately
567567
if JUPYTER_WIDGETS_ECHO:
568568
echo_state = {}
569-
for attr,value in sync_data.items():
570-
if self.trait_metadata(attr, 'echo_update', default=True):
569+
for attr, value in sync_data.items():
570+
if attr in self.keys and self.trait_metadata(attr, 'echo_update', default=True):
571571
echo_state[attr] = value
572572
if echo_state:
573573
echo_state, echo_buffer_paths, echo_buffers = _remove_buffers(echo_state)

0 commit comments

Comments
 (0)