From 87d21c65e95e8ba960254e4c6b3e702d172bc2df 2014-02-04 18:38:26 From: MinRK Date: 2014-02-04 18:38:26 Subject: [PATCH] don't allow empty selection in selection widgets raises KeyError on failed lookup (in both directions) --- diff --git a/IPython/html/widgets/widget_selection.py b/IPython/html/widgets/widget_selection.py index 64e54c5..8216419 100644 --- a/IPython/html/widgets/widget_selection.py +++ b/IPython/html/widgets/widget_selection.py @@ -71,6 +71,10 @@ class _SelectionWidget(DOMWidget): self.value_names = list(new.keys()) finally: self._in_values_changed = False + + # ensure that the chosen value is one of the choices + if self.value not in new.values(): + self.value = next(iter(new.values())) def _value_names_changed(self, name, old, new): if not self._in_values_changed: @@ -80,13 +84,13 @@ class _SelectionWidget(DOMWidget): """Called when value has been changed""" if self.value_lock.acquire(False): try: - # Make sure the value is one of the options + # Reverse dictionary lookup for the value name for k,v in self.values.items(): if new == v: # set the selected value name self.value_name = k return - raise TraitError('Value not found: %r' % new) + raise KeyError(new) finally: self.value_lock.release() @@ -94,10 +98,7 @@ class _SelectionWidget(DOMWidget): """Called when the value name has been changed (typically by the frontend).""" if self.value_lock.acquire(False): try: - if new in self.values: - self.value = self.values[new] - else: - self.value = None + self.value = self.values[new] finally: self.value_lock.release()