Skip to content

Commit cab25ca

Browse files
committed
Remove use of traitlets.Bunch
1 parent 7333885 commit cab25ca

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

ipywidgets/widgets/widget_selection.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .trait_types import InstanceDict, TypedTuple
1717
from .widget import register, widget_serialization
1818
from .docutils import doc_subst
19-
from traitlets import (Unicode, Bool, Bunch, Int, Any, Dict, TraitError, CaselessStrEnum,
19+
from traitlets import (Unicode, Bool, Int, Any, Dict, TraitError, CaselessStrEnum,
2020
Tuple, Union, observe, validate)
2121

2222
_doc_snippets = {}
@@ -94,6 +94,13 @@
9494
"""
9595

9696

97+
def _exhaust_iterable(x):
98+
"""Exhaust any non-mapping iterable into a tuple"""
99+
if isinstance(x, Iterable) and not isinstance(x, Mapping):
100+
return tuple(x)
101+
return x
102+
103+
97104
def _make_options(x):
98105
"""Standardize the options tuple format.
99106
@@ -161,7 +168,8 @@ def __init__(self, *args, **kwargs):
161168
# We have to make the basic options bookkeeping consistent
162169
# so we don't have errors the first time validators run
163170
self._initializing_traits_ = True
164-
kwargs['options'] = self._validate_options(Bunch(value = kwargs.get('options', ())))
171+
kwargs['options'] = _exhaust_iterable(kwargs.get('options', ()))
172+
self._options_full = _make_options(kwargs['options'])
165173
self._propagate_options(None)
166174

167175
# Select the first item by default, if we can
@@ -177,8 +185,7 @@ def __init__(self, *args, **kwargs):
177185
@validate('options')
178186
def _validate_options(self, proposal):
179187
# if an iterator is provided, exhaust it
180-
if isinstance(proposal.value, Iterable) and not isinstance(proposal.value, Mapping):
181-
proposal.value = tuple(proposal.value)
188+
proposal.value = _exhaust_iterable(proposal.value)
182189
# throws an error if there is a problem converting to full form
183190
self._options_full = _make_options(proposal.value)
184191
return proposal.value
@@ -302,16 +309,16 @@ def __init__(self, *args, **kwargs):
302309
# We have to make the basic options bookkeeping consistent
303310
# so we don't have errors the first time validators run
304311
self._initializing_traits_ = True
305-
kwargs['options'] = self._validate_options(Bunch(value = kwargs.get('options', ())))
312+
kwargs['options'] = _exhaust_iterable(kwargs.get('options', ()))
313+
self._options_full = _make_options(kwargs['options'])
306314
self._propagate_options(None)
307315

308316
super().__init__(*args, **kwargs)
309317
self._initializing_traits_ = False
310318

311319
@validate('options')
312320
def _validate_options(self, proposal):
313-
if isinstance(proposal.value, Iterable) and not isinstance(proposal.value, Mapping):
314-
proposal.value = tuple(proposal.value)
321+
proposal.value = _exhaust_iterable(proposal.value)
315322
# throws an error if there is a problem converting to full form
316323
self._options_full = _make_options(proposal.value)
317324
return proposal.value
@@ -513,8 +520,7 @@ def __init__(self, *args, **kwargs):
513520

514521
@validate('options')
515522
def _validate_options(self, proposal):
516-
if isinstance(proposal.value, Iterable) and not isinstance(proposal.value, Mapping):
517-
proposal.value = tuple(proposal.value)
523+
proposal.value = _exhaust_iterable(proposal.value)
518524
self._options_full = _make_options(proposal.value)
519525
if len(self._options_full) == 0:
520526
raise TraitError("Option list must be nonempty")
@@ -537,8 +543,7 @@ def __init__(self, *args, **kwargs):
537543

538544
@validate('options')
539545
def _validate_options(self, proposal):
540-
if isinstance(proposal.value, Iterable) and not isinstance(proposal.value, Mapping):
541-
proposal.value = tuple(proposal.value)
546+
proposal.value = _exhaust_iterable(proposal.value)
542547
# throws an error if there is a problem converting to full form
543548
self._options_full = _make_options(proposal.value)
544549
if len(self._options_full) == 0:

0 commit comments

Comments
 (0)