python / python/cpython

argparse - make return type from `add_subparsers` public

Open
#101,503 10 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-typing type-feature
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

This issue was brought up and closed as part of issue # 41592.

The add_subparsers method returns a protected _SubParsersAction instance. To add a sub-parser, the public add_parser method of the protected _SubParsersAction instance must be used.

  1. It is unexpected to require the use of a public method of a protected class instance.
  2. The protected return type breaks type hinting because
    1. protected and should not be imported
    2. it is not available for import

Real world use case example:

sub_parsers_action = parser.add_subparsers(...)

def build_subcmd(sub_parsers_action: _SubParsersAction):
    ...

The ask is to make the type of whatever is returned from add_subparsers public.

Linked PRs
  • gh-157001

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at argparse.ArgumentParser.add_subparsers and trace the returned _SubParsersAction, including its public add_parser method. Review linked PR gh-157001 and the surrounding argparse typing/API context; done means the returned type is publicly usable for type hints without importing the protected class.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.