馃悰 fix: positional arguments declared before add_subparsers() - #347
Merged
Conversation
gaborbernat
force-pushed
the
subparsers-positional
branch
6 times, most recently
from
August 27, 2026 16:25
1c2c977 to
2595ebc
Compare
A positional argument added before add_subparsers() made `_group_actions[0]` a store action, and the directive crashed with AttributeError while loading sub-commands. The same index assumption skipped the whole positional group of the root parser whenever sub-commands existed, so those positionals never rendered, and a group whose actions were all SUPPRESS still produced an empty section. Locate the sub-parsers action by isinstance over the parser's actions and filter each group down to its visible actions before rendering it. The three call sites that hard-coded index zero now share one helper.
gaborbernat
force-pushed
the
subparsers-positional
branch
from
August 27, 2026 16:30
2595ebc to
80fba54
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A parser that calls
add_argument("root")beforeadd_subparsers()crashed the build withAttributeError: '_StoreAction' object has no attribute '_name_parser_map'. The directive read_group_actions[0]of the positional group and assumed the sub-parsers action lived there. 馃悰 That assumption also hid the root parser's positionals whenever sub-commands existed, because the code skipped the whole positional group, and a group whose arguments all carriedhelp=SUPPRESSstill got a heading over an empty list.The directive now finds the sub-parsers action by type across the parser's actions. Each group filters down to its visible actions, meaning not suppressed and not the sub-parsers action, and only then earns a section. Three call sites shared the index-zero assumption; they now call one helper, which also drops the
type: ignoreeach of them carried.Sub-command headings keep the invocation argparse reports, positional included (
prog root run), as both title and anchor.{subcommand}in:group_sub_title_prefix:still takes the first token after the program name, so a leading positional shows up there instead of the sub-command. That derivation predates this change; a separate PR can address it.