@@ -896,9 +896,9 @@ def attach_subcommand(
896896 :param subcommand: name of the new subcommand
897897 :param subcommand_parser: the parser to attach
898898 :param add_parser_kwargs: additional arguments for the subparser registration (e.g. help, aliases)
899- :raises TypeError: if the subcommand parser is not an instance of 'Cmd2ArgumentParser'
900- (or one of its subclasses), or if its type does not match the 'parser_class'
901- configured for the target subcommand group.
899+ :raises TypeError: if subcommand_parser is not an instance of the following or their subclasses:
900+ 1. Cmd2ArgumentParser
901+ 2. The parser_class configured for the target subcommand group
902902 :raises ValueError: if the command path is invalid or doesn't support subcommands
903903 """
904904 if not isinstance (subcommand_parser , Cmd2ArgumentParser ):
@@ -910,12 +910,15 @@ def attach_subcommand(
910910 target_parser = self ._find_parser (subcommand_path )
911911 subparsers_action = target_parser ._get_subparsers_action ()
912912
913- # Mirror argparse's add_parser() behavior by requiring an exact type match with _parser_class
914- if type (subcommand_parser ) is not subparsers_action ._parser_class :
913+ # Verify the parser is compatible with the 'parser_class' configured for this
914+ # subcommand group. We use isinstance() here to allow for subclasses, providing
915+ # more flexibility than the standard add_parser() factory approach which enforces
916+ # a specific class.
917+ if not isinstance (subcommand_parser , subparsers_action ._parser_class ):
915918 raise TypeError (
916- f"The attached parser must be of type '{ subparsers_action ._parser_class .__name__ } ' "
917- f"to match the 'parser_class' configured for this subparsers action . "
918- f"Received '{ type (subcommand_parser ).__name__ } '."
919+ f"The attached parser must be an instance of '{ subparsers_action ._parser_class .__name__ } ' "
920+ f"(or a subclass) to match the 'parser_class' configured for this subcommand group . "
921+ f"Received: '{ type (subcommand_parser ).__name__ } '."
919922 )
920923
921924 # Use add_parser to register the subcommand name and any aliases
0 commit comments