16
16
from .trait_types import InstanceDict , TypedTuple
17
17
from .widget import register , widget_serialization
18
18
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 ,
20
20
Tuple , Union , observe , validate )
21
21
22
22
_doc_snippets = {}
94
94
"""
95
95
96
96
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
+
97
104
def _make_options (x ):
98
105
"""Standardize the options tuple format.
99
106
@@ -161,7 +168,8 @@ def __init__(self, *args, **kwargs):
161
168
# We have to make the basic options bookkeeping consistent
162
169
# so we don't have errors the first time validators run
163
170
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' ])
165
173
self ._propagate_options (None )
166
174
167
175
# Select the first item by default, if we can
@@ -177,8 +185,7 @@ def __init__(self, *args, **kwargs):
177
185
@validate ('options' )
178
186
def _validate_options (self , proposal ):
179
187
# 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 )
182
189
# throws an error if there is a problem converting to full form
183
190
self ._options_full = _make_options (proposal .value )
184
191
return proposal .value
@@ -302,16 +309,16 @@ def __init__(self, *args, **kwargs):
302
309
# We have to make the basic options bookkeeping consistent
303
310
# so we don't have errors the first time validators run
304
311
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' ])
306
314
self ._propagate_options (None )
307
315
308
316
super ().__init__ (* args , ** kwargs )
309
317
self ._initializing_traits_ = False
310
318
311
319
@validate ('options' )
312
320
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 )
315
322
# throws an error if there is a problem converting to full form
316
323
self ._options_full = _make_options (proposal .value )
317
324
return proposal .value
@@ -513,8 +520,7 @@ def __init__(self, *args, **kwargs):
513
520
514
521
@validate ('options' )
515
522
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 )
518
524
self ._options_full = _make_options (proposal .value )
519
525
if len (self ._options_full ) == 0 :
520
526
raise TraitError ("Option list must be nonempty" )
@@ -537,8 +543,7 @@ def __init__(self, *args, **kwargs):
537
543
538
544
@validate ('options' )
539
545
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 )
542
547
# throws an error if there is a problem converting to full form
543
548
self ._options_full = _make_options (proposal .value )
544
549
if len (self ._options_full ) == 0 :
0 commit comments