From b38fd03cf077a990b2208b6bc796c4122ff1b6f5 Mon Sep 17 00:00:00 2001 From: Jonathan Conder Date: Thu, 31 Jul 2025 17:06:29 +1200 Subject: [PATCH] fix: filter other commands by name instead of type Previously, a command like craft_appplication.command.TestCommand, which inherits from PackCommand, would omit the parent command in the "see also" section. --- craft_cli/helptexts.py | 2 +- tests/factory.py | 5 +++-- tests/unit/test_help.py | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/craft_cli/helptexts.py b/craft_cli/helptexts.py index 86859a6a..191540b8 100644 --- a/craft_cli/helptexts.py +++ b/craft_cli/helptexts.py @@ -475,7 +475,7 @@ def get_command_help( else: raise RuntimeError("Internal inconsistency in commands groups") other_command_names = [ - c.name for c in command_group.commands if not isinstance(command, c) + c.name for c in command_group.commands if c.name != command.name ] if output_format == OutputFormat.markdown: diff --git a/tests/factory.py b/tests/factory.py index 4fc23e1d..aa58a05e 100644 --- a/tests/factory.py +++ b/tests/factory.py @@ -25,7 +25,8 @@ def create_command( hidden: bool = False, overview: str = "", class_name: str = "MyCommand", -) -> type["BaseCommand"]: + base_class: type[BaseCommand] = BaseCommand, +) -> type[BaseCommand]: """Helper to create commands.""" attribs = { "name": name, @@ -36,4 +37,4 @@ def create_command( "needs_config": False, "run": lambda parsed_args: None, } - return type(class_name, (BaseCommand,), attribs) + return type(class_name, (base_class,), attribs) diff --git a/tests/unit/test_help.py b/tests/unit/test_help.py index 5bee65c4..352d0d80 100644 --- a/tests/unit/test_help.py +++ b/tests/unit/test_help.py @@ -443,8 +443,11 @@ def test_command_help_text_no_parameters(docs_url, output_format): Multiline! """ ) - cmd1 = create_command("somecommand", "Command one line help.", overview=overview) cmd2 = create_command("other-cmd-2", "Some help.") + # Inherit from cmd2 to check that superclasses are treated as different commands. + cmd1 = create_command( + "somecommand", "Command one line help.", overview=overview, base_class=cmd2 + ) cmd3 = create_command("other-cmd-3", "Some help.") cmd4 = create_command("other-cmd-4", "Some help.") command_groups = [