1- # coding=utf-8
2- # flake8: noqa C901
3- # NOTE: Ignoring flake8 cyclomatic complexity in this file
41"""
52This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
63See the header of argparse_custom.py for instructions on how to use these features.
1411)
1512from typing import (
1613 TYPE_CHECKING ,
17- Dict ,
18- List ,
1914 Optional ,
2015 Type ,
2116 Union ,
@@ -172,7 +167,7 @@ class ArgparseCompleter:
172167 """Automatic command line tab completion based on argparse parameters"""
173168
174169 def __init__ (
175- self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : Optional [Dict [str , List [str ]]] = None
170+ self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : Optional [dict [str , list [str ]]] = None
176171 ) -> None :
177172 """
178173 Create an ArgparseCompleter
@@ -187,7 +182,7 @@ def __init__(
187182 self ._cmd2_app = cmd2_app
188183
189184 if parent_tokens is None :
190- parent_tokens = dict ()
185+ parent_tokens = {}
191186 self ._parent_tokens = parent_tokens
192187
193188 self ._flags = [] # all flags in this command
@@ -213,8 +208,8 @@ def __init__(
213208 self ._subcommand_action = action
214209
215210 def complete (
216- self , text : str , line : str , begidx : int , endidx : int , tokens : List [str ], * , cmd_set : Optional [CommandSet ] = None
217- ) -> List [str ]:
211+ self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ], * , cmd_set : Optional [CommandSet ] = None
212+ ) -> list [str ]:
218213 """
219214 Complete text using argparse metadata
220215
@@ -245,13 +240,13 @@ def complete(
245240 flag_arg_state : Optional [_ArgumentState ] = None
246241
247242 # Non-reusable flags that we've parsed
248- matched_flags : List [str ] = []
243+ matched_flags : list [str ] = []
249244
250245 # Keeps track of arguments we've seen and any tokens they consumed
251- consumed_arg_values : Dict [str , List [str ]] = dict () # dict(arg_name -> List [tokens])
246+ consumed_arg_values : dict [str , list [str ]] = {} # dict(arg_name -> list [tokens])
252247
253248 # Completed mutually exclusive groups
254- completed_mutex_groups : Dict [argparse ._MutuallyExclusiveGroup , argparse .Action ] = dict ()
249+ completed_mutex_groups : dict [argparse ._MutuallyExclusiveGroup , argparse .Action ] = {}
255250
256251 def consume_argument (arg_state : _ArgumentState ) -> None :
257252 """Consuming token as an argument"""
@@ -507,7 +502,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
507502
508503 return completion_results
509504
510- def _complete_flags (self , text : str , line : str , begidx : int , endidx : int , matched_flags : List [str ]) -> List [str ]:
505+ def _complete_flags (self , text : str , line : str , begidx : int , endidx : int , matched_flags : list [str ]) -> list [str ]:
511506 """Tab completion routine for a parsers unused flags"""
512507
513508 # Build a list of flags that can be tab completed
@@ -524,7 +519,7 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
524519 matches = self ._cmd2_app .basic_complete (text , line , begidx , endidx , match_against )
525520
526521 # Build a dictionary linking actions with their matched flag names
527- matched_actions : Dict [argparse .Action , List [str ]] = dict ()
522+ matched_actions : dict [argparse .Action , list [str ]] = {}
528523 for flag in matches :
529524 action = self ._flag_to_action [flag ]
530525 matched_actions .setdefault (action , [])
@@ -541,14 +536,14 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
541536
542537 return matches
543538
544- def _format_completions (self , arg_state : _ArgumentState , completions : Union [List [str ], List [CompletionItem ]]) -> List [str ]:
539+ def _format_completions (self , arg_state : _ArgumentState , completions : Union [list [str ], list [CompletionItem ]]) -> list [str ]:
545540 """Format CompletionItems into hint table"""
546541
547542 # Nothing to do if we don't have at least 2 completions which are all CompletionItems
548543 if len (completions ) < 2 or not all (isinstance (c , CompletionItem ) for c in completions ):
549- return cast (List [str ], completions )
544+ return cast (list [str ], completions )
550545
551- completion_items = cast (List [CompletionItem ], completions )
546+ completion_items = cast (list [CompletionItem ], completions )
552547
553548 # Check if the data being completed have a numerical type
554549 all_nums = all (isinstance (c .orig_value , numbers .Number ) for c in completion_items )
@@ -599,7 +594,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List
599594 item .description = item .description .replace ('\t ' , four_spaces )
600595 desc_width = max (widest_line (item .description ), desc_width )
601596
602- cols = list ()
597+ cols = []
603598 dest_alignment = HorizontalAlignment .RIGHT if all_nums else HorizontalAlignment .LEFT
604599 cols .append (
605600 Column (
@@ -616,17 +611,17 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List
616611 self ._cmd2_app .formatted_completions = hint_table .generate_table (table_data , row_spacing = 0 )
617612
618613 # Return sorted list of completions
619- return cast (List [str ], completions )
614+ return cast (list [str ], completions )
620615
621- def complete_subcommand_help (self , text : str , line : str , begidx : int , endidx : int , tokens : List [str ]) -> List [str ]:
616+ def complete_subcommand_help (self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ]) -> list [str ]:
622617 """
623618 Supports cmd2's help command in the completion of subcommand names
624619 :param text: the string prefix we are attempting to match (all matches must begin with it)
625620 :param line: the current input line with leading whitespace removed
626621 :param begidx: the beginning index of the prefix text
627622 :param endidx: the ending index of the prefix text
628623 :param tokens: arguments passed to command/subcommand
629- :return: List of subcommand completions
624+ :return: list of subcommand completions
630625 """
631626 # If our parser has subcommands, we must examine the tokens and check if they are subcommands
632627 # If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
@@ -645,7 +640,7 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
645640 break
646641 return []
647642
648- def format_help (self , tokens : List [str ]) -> str :
643+ def format_help (self , tokens : list [str ]) -> str :
649644 """
650645 Supports cmd2's help command in the retrieval of help text
651646 :param tokens: arguments passed to help command
@@ -672,17 +667,17 @@ def _complete_arg(
672667 begidx : int ,
673668 endidx : int ,
674669 arg_state : _ArgumentState ,
675- consumed_arg_values : Dict [str , List [str ]],
670+ consumed_arg_values : dict [str , list [str ]],
676671 * ,
677672 cmd_set : Optional [CommandSet ] = None ,
678- ) -> List [str ]:
673+ ) -> list [str ]:
679674 """
680675 Tab completion routine for an argparse argument
681676 :return: list of completions
682677 :raises CompletionError: if the completer or choices function this calls raises one
683678 """
684679 # Check if the arg provides choices to the user
685- arg_choices : Union [List [str ], ChoicesCallable ]
680+ arg_choices : Union [list [str ], ChoicesCallable ]
686681 if arg_state .action .choices is not None :
687682 arg_choices = list (arg_state .action .choices )
688683 if not arg_choices :
@@ -739,7 +734,7 @@ def _complete_arg(
739734 # Otherwise use basic_complete on the choices
740735 else :
741736 # Check if the choices come from a function
742- completion_items : List [str ] = []
737+ completion_items : list [str ] = []
743738 if isinstance (arg_choices , ChoicesCallable ):
744739 if not arg_choices .is_completer :
745740 choices_func = arg_choices .choices_provider
0 commit comments