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,13 +168,13 @@ 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
- options = _make_options (kwargs .get ('options' , ()))
165
- self ._options_full = options
166
- self .set_trait ('_options_labels' , tuple (i [0 ] for i in options ))
167
- self ._options_values = tuple (i [1 ] for i in options )
171
+ kwargs ['options' ] = _exhaust_iterable (kwargs .get ('options' , ()))
172
+ self ._options_full = _make_options (kwargs ['options' ])
173
+ self ._propagate_options (None )
168
174
169
175
# Select the first item by default, if we can
170
176
if 'index' not in kwargs and 'value' not in kwargs and 'label' not in kwargs :
177
+ options = self ._options_full
171
178
nonempty = (len (options ) > 0 )
172
179
kwargs ['index' ] = 0 if nonempty else None
173
180
kwargs ['label' ], kwargs ['value' ] = options [0 ] if nonempty else (None , None )
@@ -178,8 +185,7 @@ def __init__(self, *args, **kwargs):
178
185
@validate ('options' )
179
186
def _validate_options (self , proposal ):
180
187
# if an iterator is provided, exhaust it
181
- if isinstance (proposal .value , Iterable ) and not isinstance (proposal .value , Mapping ):
182
- proposal .value = tuple (proposal .value )
188
+ proposal .value = _exhaust_iterable (proposal .value )
183
189
# throws an error if there is a problem converting to full form
184
190
self ._options_full = _make_options (proposal .value )
185
191
return proposal .value
@@ -303,18 +309,16 @@ def __init__(self, *args, **kwargs):
303
309
# We have to make the basic options bookkeeping consistent
304
310
# so we don't have errors the first time validators run
305
311
self ._initializing_traits_ = True
306
- options = _make_options (kwargs .get ('options' , ()))
307
- self ._full_options = options
308
- self .set_trait ('_options_labels' , tuple (i [0 ] for i in options ))
309
- self ._options_values = tuple (i [1 ] for i in options )
312
+ kwargs ['options' ] = _exhaust_iterable (kwargs .get ('options' , ()))
313
+ self ._options_full = _make_options (kwargs ['options' ])
314
+ self ._propagate_options (None )
310
315
311
316
super ().__init__ (* args , ** kwargs )
312
317
self ._initializing_traits_ = False
313
318
314
319
@validate ('options' )
315
320
def _validate_options (self , proposal ):
316
- if isinstance (proposal .value , Iterable ) and not isinstance (proposal .value , Mapping ):
317
- proposal .value = tuple (proposal .value )
321
+ proposal .value = _exhaust_iterable (proposal .value )
318
322
# throws an error if there is a problem converting to full form
319
323
self ._options_full = _make_options (proposal .value )
320
324
return proposal .value
@@ -516,8 +520,7 @@ def __init__(self, *args, **kwargs):
516
520
517
521
@validate ('options' )
518
522
def _validate_options (self , proposal ):
519
- if isinstance (proposal .value , Iterable ) and not isinstance (proposal .value , Mapping ):
520
- proposal .value = tuple (proposal .value )
523
+ proposal .value = _exhaust_iterable (proposal .value )
521
524
self ._options_full = _make_options (proposal .value )
522
525
if len (self ._options_full ) == 0 :
523
526
raise TraitError ("Option list must be nonempty" )
@@ -540,8 +543,7 @@ def __init__(self, *args, **kwargs):
540
543
541
544
@validate ('options' )
542
545
def _validate_options (self , proposal ):
543
- if isinstance (proposal .value , Iterable ) and not isinstance (proposal .value , Mapping ):
544
- proposal .value = tuple (proposal .value )
546
+ proposal .value = _exhaust_iterable (proposal .value )
545
547
# throws an error if there is a problem converting to full form
546
548
self ._options_full = _make_options (proposal .value )
547
549
if len (self ._options_full ) == 0 :
0 commit comments