gh-144812: ✨ Add public argparse group typing protocols#150597
Open
gaborbernat wants to merge 1 commit into
Open
gh-144812: ✨ Add public argparse group typing protocols#150597gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
Documentation build overview
|
ArgumentParser.add_argument_group() and add_mutually_exclusive_group() return objects whose only public name was the private _ArgumentGroup and _MutuallyExclusiveGroup. Code that stores or passes these objects had no public type to annotate against, forcing callers to reference the underscore-prefixed implementation classes. Expose them as structural protocols rather than aliasing the concrete classes, so the implementation stays private and free to evolve while callers get a stable contract to annotate against. The protocols are built on first attribute access through the module __getattr__, keeping typing off the import path: argparse sits on the start-up path of most CLIs and does not otherwise import typing.
22dfcb6 to
6ab9015
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.
ArgumentParser.add_argument_group()andadd_mutually_exclusive_group()return objects whose only name is the private_ArgumentGroupand_MutuallyExclusiveGroup. Code that stores or passes these objects around has no public type to annotate against, so callers reach for the underscore-prefixed implementation classes. A GitHub code search turns up roughly 140 projects doing exactly that. 📦This exposes two public names,
argparse.ArgumentGroupandargparse.MutuallyExclusiveGroup, as structuraltyping.Protocols that guarantee theadd_argument()method callers rely on. Modelling them as protocols rather than aliasing the concrete classes keeps the implementation private and free to evolve, which addresses the concern raised on the issue that typing needs should not turn inner classes into frozen public API. ✨ The protocols are built on first attribute access through the module__getattr__, soimport argparsestays off thetypingimport path — relevant becauseargparsesits on the start-up path of most CLIs and does not otherwise importtyping.Type checkers read typeshed rather than
Lib/argparse.py, so these names take effect for end users oncestdlib/argparse.pyideclares the protocols and points the two return types at them. I confirmed that change makes mypy, pyright, ty, and pyrefly all resolve the public names; a matchingpython/typeshedPR will follow.