Skip to content

Commit ae04e42

Browse files
committed
No longer storing Cmd/CommandSet instance in subcommand parsers. Using id(instance) instead.
1 parent 943c4e3 commit ae04e42

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,12 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:
950950

951951
def _check_uninstallable(self, cmdset: CommandSet) -> None:
952952
def check_parser_uninstallable(parser: argparse.ArgumentParser) -> None:
953+
cmdset_id = id(cmdset)
953954
for action in parser._actions:
954955
if isinstance(action, argparse._SubParsersAction):
955956
for subparser in action.choices.values():
956-
attached_cmdset = getattr(subparser, constants.PARSER_ATTR_COMMANDSET, None)
957-
if attached_cmdset is not None and attached_cmdset is not cmdset:
957+
attached_cmdset_id = getattr(subparser, constants.PARSER_ATTR_COMMANDSET_ID, None)
958+
if attached_cmdset_id is not None and attached_cmdset_id != cmdset_id:
958959
raise CommandSetRegistrationError(
959960
'Cannot uninstall CommandSet when another CommandSet depends on it'
960961
)
@@ -1049,7 +1050,7 @@ def find_subcommand(action: argparse.ArgumentParser, subcmd_names: list[str]) ->
10491050
subcmd_parser.set_defaults(**defaults)
10501051

10511052
# Set what instance the handler is bound to
1052-
setattr(subcmd_parser, constants.PARSER_ATTR_COMMANDSET, cmdset)
1053+
setattr(subcmd_parser, constants.PARSER_ATTR_COMMANDSET_ID, id(cmdset))
10531054

10541055
# Find the argparse action that handles subcommands
10551056
for action in target_parser._actions:

cmd2/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
SUBCMD_ATTR_NAME = 'subcommand_name'
4848
SUBCMD_ATTR_ADD_PARSER_KWARGS = 'subcommand_add_parser_kwargs'
4949

50-
# arpparse attribute linking to command set instance
51-
PARSER_ATTR_COMMANDSET = 'command_set'
50+
# arpparse attribute uniquely identifying the command set instance
51+
PARSER_ATTR_COMMANDSET_ID = 'command_set_id'
5252

5353
# custom attributes added to argparse Namespaces
5454
NS_ATTR_SUBCMD_HANDLER = '__subcmd_handler__'

0 commit comments

Comments
 (0)