Skip to content

Commit d9c2c34

Browse files
committed
Removed defaultdicts to simplify some code.
1 parent 697da11 commit d9c2c34

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

cmd2/argparse_completer.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import argparse
77
import dataclasses
88
import inspect
9-
from collections import (
10-
defaultdict,
11-
deque,
12-
)
9+
from collections import deque
1310
from 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] = []

cmd2/cmd2.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import time
4343
from code import InteractiveConsole
4444
from collections import (
45-
defaultdict,
4645
deque,
4746
namedtuple,
4847
)
@@ -4223,7 +4222,7 @@ def _build_command_info(self) -> tuple[dict[str, list[str]], list[str], list[str
42234222

42244223
# Get a sorted list of visible command names
42254224
visible_commands = sorted(self.get_visible_commands(), key=utils.DEFAULT_STR_SORT_KEY)
4226-
cmds_cats: dict[str, list[str]] = defaultdict(list)
4225+
cmds_cats: dict[str, list[str]] = {}
42274226
cmds_undoc: list[str] = []
42284227

42294228
for command in visible_commands:
@@ -4238,16 +4237,21 @@ def _build_command_info(self) -> tuple[dict[str, list[str]], list[str], list[str
42384237
# Non-argparse commands can have help_functions for their documentation
42394238
has_help_func = not has_parser
42404239

4240+
# Determine the category
4241+
category: str | None = None
4242+
42414243
if hasattr(func, constants.CMD_ATTR_HELP_CATEGORY):
4242-
category: str = getattr(func, constants.CMD_ATTR_HELP_CATEGORY)
4243-
cmds_cats[category].append(command)
4244+
category = getattr(func, constants.CMD_ATTR_HELP_CATEGORY)
42444245
elif func.__doc__ or has_help_func or has_parser:
4245-
# Determine the category based on the defining class
42464246
defining_cls = get_defining_class(func)
42474247
category = getattr(defining_cls, 'DEFAULT_CATEGORY', self.DEFAULT_CATEGORY)
4248-
cmds_cats[category].append(command)
4248+
4249+
# Store the command
4250+
if category is not None:
4251+
cmds_cats.setdefault(category, []).append(command)
42494252
else:
42504253
cmds_undoc.append(command)
4254+
42514255
return cmds_cats, cmds_undoc, help_topics
42524256

42534257
@classmethod

0 commit comments

Comments
 (0)