Make _MutuallyExclusiveGroup a public API in argparse
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
argparse.ArgumentParser.add_mutually_exclusive_group() returns an argparse._MutuallyExclusiveGroup instance, but this type is private (underscore-prefixed). This makes it impossible to properly type-annotate code that stores the return value without importing a private symbol.
Motivation
When using type checkers (mypy, ty, pyright), code like:
group = parser.add_argument_group("selection")
add_to: ArgumentParser = group.add_mutually_exclusive_group(required=False)
is flagged as a type error because _MutuallyExclusiveGroup is not ArgumentParser, but there's no public type to use instead. The only workaround is importing the private _MutuallyExclusiveGroup.
This is similar to #101503 which requests making the return type of add_subparsers public.
Proposal
Make _MutuallyExclusiveGroup (and possibly _ArgumentGroup) public by removing the underscore prefix, or at minimum export a public type alias.
Linked PRs
- gh-150597
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with argparse.ArgumentParser.add_mutually_exclusive_group() and review linked PR gh-150597, which represents work already underway. Compare the proposed public name or alias with the type-checking example in the issue; done means users can annotate the returned group without importing a private symbol.
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
- 25/100