2121)
2222
2323
24- from trogon .schemas import ArgumentSchema , MultiValueParamData , OptionSchema
24+ from trogon .schemas import ArgumentSchema , MultiValueParamData , OptionSchema , ChoiceSchema
2525from trogon .widgets .multiple_choice import MultipleChoice
2626
2727ControlWidgetType : TypeVar = Union [Input , Checkbox , MultipleChoice , Select ]
@@ -135,7 +135,9 @@ def compose(self) -> ComposeResult:
135135 # There's a special case where we have a Choice with multiple=True,
136136 # in this case, we can just render a single MultipleChoice widget
137137 # instead of multiple radio-sets.
138- control_method = self .get_control_method (schema = schema )
138+ control_method = self .get_control_method (
139+ param_type = ChoiceSchema (choices = schema .choices )
140+ )
139141 multiple_choice_widget = control_method (
140142 default = default ,
141143 label = label ,
@@ -187,7 +189,7 @@ def compose(self) -> ComposeResult:
187189 # If it's a multiple, and it's a Choice parameter, then we display
188190 # our special case MultiChoice widget, and so there's no need for this
189191 # button.
190- if multiple or nargs == - 1 and not schema .choices :
192+ if ( multiple or nargs == - 1 ) and not schema .choices :
191193 with Horizontal (classes = "add-another-button-container" ):
192194 yield Button ("+ value" , variant = "success" , classes = "add-another-button" )
193195
@@ -209,12 +211,20 @@ def make_widget_group(self) -> Iterable[Widget]:
209211 )
210212
211213 # Get the types of the parameter. We can map these types on to widgets that will be rendered.
212- parameter_types = [parameter_type ] * schema .nargs if schema .nargs > 1 else [parameter_type ]
214+ parameter_types = [
215+ parameter_type [i ] if i < len (parameter_type ) else parameter_type [- 1 ]
216+ for i in range (schema .nargs if schema .nargs > 1 else 1 )
217+ ]
218+ # The above ensures that len(parameter_types) == nargs.
219+ # if there are more parameter_types than args, parameter_types is truncated.
220+ # if there are fewer parameter_types than args, the *last* parameter type is repeated as much as necessary.
213221
214222 # For each of the these parameters, render the corresponding widget for it.
215223 # At this point we don't care about filling in the default values.
216224 for _type in parameter_types :
217- control_method = self .get_control_method (schema = schema )
225+ if schema .choices :
226+ _type = ChoiceSchema (choices = schema .choices )
227+ control_method = self .get_control_method (param_type = _type )
218228 control_widgets = control_method (
219229 default , label , multiple , schema , schema .key
220230 )
@@ -301,12 +311,12 @@ def list_to_tuples(
301311 return MultiValueParamData .process_cli_option (collected_values )
302312
303313 def get_control_method (
304- self , schema : ArgumentSchema
314+ self , param_type : Type [ Any ],
305315 ) -> Callable [[Any , Text , bool , OptionSchema | ArgumentSchema , str ], Widget ]:
306- if schema . choices :
307- return partial (self .make_choice_control , choices = schema .choices )
316+ if isinstance ( param_type , ChoiceSchema ) :
317+ return partial (self .make_choice_control , choices = param_type .choices )
308318
309- if schema . type is bool :
319+ if param_type is bool :
310320 return self .make_checkbox_control
311321
312322 return self .make_text_control
@@ -382,7 +392,7 @@ def make_choice_control(
382392 @staticmethod
383393 def _make_command_form_control_label (
384394 name : str | list [str ],
385- type : Type [Any ],
395+ types : list [ Type [Any ] ],
386396 is_option : bool ,
387397 is_required : bool ,
388398 multiple : bool ,
@@ -391,7 +401,7 @@ def _make_command_form_control_label(
391401
392402 names = Text (" / " , style = "dim" ).join ([Text (n ) for n in names ])
393403 text = Text .from_markup (
394- f"{ names } [dim]{ ' multiple' if multiple else '' } <{ type . __name__ } >[/] { ' [b red]*[/]required' if is_required else '' } "
404+ f"{ names } [dim]{ ' multiple' if multiple else '' } <{ ', ' . join ( x . __name__ for x in types ) } >[/] { ' [b red]*[/]required' if is_required else '' } "
395405 )
396406
397407 return text
0 commit comments