Skip to content

馃悰 fix: positional arguments declared before add_subparsers() - #347

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:subparsers-positional
Aug 27, 2026
Merged

馃悰 fix: positional arguments declared before add_subparsers()#347
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:subparsers-positional

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Aug 27, 2026

Copy link
Copy Markdown
Member

A parser that calls add_argument("root") before add_subparsers() crashed the build with AttributeError: '_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 carried help=SUPPRESS still 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: ignore each 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.

@gaborbernat gaborbernat added the bug Something isn't working label Aug 27, 2026
@gaborbernat
gaborbernat force-pushed the subparsers-positional branch 6 times, most recently from 1c2c977 to 2595ebc Compare August 27, 2026 16:25
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
gaborbernat force-pushed the subparsers-positional branch from 2595ebc to 80fba54 Compare August 27, 2026 16:30
@gaborbernat
gaborbernat merged commit 98467e5 into tox-dev:main Aug 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant