1313V = TypeVar ('V' , bound = 'Union[tuple[Option[Any, Any], ...], Optional[Option[Any, Any]]]' )
1414
1515
16+ def _convert_value (value : Union [tuple [T , ...], Optional [T ], tuple [P , ...], Optional [P ], Optional [Option [P , P ]], tuple [Option [P , P ], ...]]):
17+ if isinstance (value , tuple ) and all (isinstance (v , (str , int , float , bool )) for v in value ):
18+ return tuple (to_option (v ) for v in value )
19+ if isinstance (value , (str , int , float , bool )):
20+ return to_option (value )
21+ return value
22+
23+
24+ def _convert_options ( options : Union [Iterable [T ], Iterable [P ]]):
25+ if all (isinstance (v , (str , int , float , bool )) for v in options ):
26+ return [to_option (v ) for v in options ]
27+ return options
28+
29+
1630class Select (
1731 LabelElement ,
1832 ValidationElement [V ],
@@ -73,14 +87,7 @@ def __init__(self,
7387 self .new_value_to_option : Optional [Callable [[str ], Optional [T ]]] = new_value_to_option
7488 if self .new_value_mode and self .new_value_to_option is None :
7589 raise ValueError (f"new_value_to_option not passed. You must provide a function for handling new values when new value mode is '{ self .new_value_mode } '." )
76- if isinstance (value , tuple ) and all (isinstance (v , (str , int , float , bool )) for v in value ):
77- value = tuple (to_option (v ) for v in value )
78- if isinstance (value , (str , int , float , bool )):
79- value = to_option (value )
80- if all (isinstance (v , (str , int , float , bool )) for v in options ):
81- super ().__init__ (label = label or None , options = [to_option (v ) for v in options ], value = value , on_change = on_change , validation = validation )
82- else :
83- super ().__init__ (label = label or None , options = options , value = value , on_change = on_change , validation = validation )
90+ super ().__init__ (label = label or None , options = _convert_options (options ), value = _convert_value (value ), on_change = on_change , validation = validation )
8491 if new_value_mode is not None :
8592 self ._props ['new-value-mode' ] = new_value_mode
8693 with_input = True
@@ -146,6 +153,7 @@ def _handle_new_value(self, value: str) -> Optional[T]:
146153 self ._update_values_and_labels ()
147154 return new_option
148155
156+
149157@overload
150158def select (
151159 options : Iterable [T ], * , label : str = ..., value : tuple [T , ...],
@@ -158,7 +166,6 @@ def select(
158166 ) -> Select [tuple [T , ...], T ]:
159167 ...
160168
161-
162169@overload
163170def select (
164171 options : Iterable [T ], * , label : str = ..., value : Literal [None ] = ...,
@@ -171,7 +178,6 @@ def select(
171178 ) -> Select [Optional [T ], T ]:
172179 ...
173180
174-
175181@overload
176182def select (
177183 options : Iterable [P ], * , label : str = ..., value : tuple [P , ...],
0 commit comments