66import argparse
77import dataclasses
88import inspect
9- from collections import (
10- defaultdict ,
11- deque ,
12- )
9+ from collections import deque
1310from collections .abc import (
1411 Mapping ,
1512 MutableSequence ,
@@ -251,15 +248,15 @@ def complete(
251248 used_flags : set [str ] = set ()
252249
253250 # Keeps track of arguments we've seen and any tokens they consumed
254- consumed_arg_values : dict [str , list [str ]] = defaultdict ( list )
251+ consumed_arg_values : dict [str , list [str ]] = {}
255252
256253 # Completed mutually exclusive groups
257254 completed_mutex_groups : dict [argparse ._MutuallyExclusiveGroup , argparse .Action ] = {}
258255
259256 def consume_argument (arg_state : _ArgumentState , arg_token : str ) -> None :
260257 """Consume token as an argument."""
261258 arg_state .count += 1
262- consumed_arg_values [ arg_state .action .dest ] .append (arg_token )
259+ consumed_arg_values . setdefault ( arg_state .action .dest , []) .append (arg_token )
263260
264261 #############################################################################################
265262 # Parse all but the last token
@@ -336,7 +333,7 @@ def consume_argument(arg_state: _ArgumentState, arg_token: str) -> None:
336333 # filter them from future completion results and clear any previously
337334 # recorded values for this destination.
338335 used_flags .update (action .option_strings )
339- consumed_arg_values [action .dest ]. clear ()
336+ consumed_arg_values [action .dest ] = []
340337
341338 new_arg_state = _ArgumentState (action )
342339
@@ -362,7 +359,6 @@ def consume_argument(arg_state: _ArgumentState, arg_token: str) -> None:
362359 # Are we at a subcommand? If so, forward to the matching completer
363360 if self ._subcommand_action is not None and action == self ._subcommand_action :
364361 if token in self ._subcommand_action .choices :
365- # Merge self._parent_tokens and consumed_arg_values
366362 parent_tokens = {** self ._parent_tokens , ** consumed_arg_values }
367363
368364 # Include the subcommand name if its destination was set
@@ -557,15 +553,15 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, used_f
557553 match_against .append (flag )
558554
559555 # Build a dictionary linking actions with their matched flag names
560- matched_actions : dict [argparse .Action , list [str ]] = defaultdict ( list )
556+ matched_actions : dict [argparse .Action , list [str ]] = {}
561557
562558 # Keep flags sorted in the order provided by argparse so our completion
563559 # suggestions display the same as argparse help text.
564560 matched_flags = self ._cmd2_app .basic_complete (text , line , begidx , endidx , match_against , sort = False )
565561
566562 for flag in matched_flags .to_strings ():
567563 action = self ._flag_to_action [flag ]
568- matched_actions [ action ] .append (flag )
564+ matched_actions . setdefault ( action , []) .append (flag )
569565
570566 # For completion suggestions, group matched flags by action
571567 items : list [CompletionItem ] = []
0 commit comments