Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-69223: Document that add_argument returns an Action object#144452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
kovan wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromkovan:gh-69223-add_argument-returns-action

Conversation

@kovan
Copy link

@kovankovan commentedFeb 3, 2026
edited by github-actionsbot
Loading

Summary

  • Documents thatArgumentParser.add_argument() returns anAction object
  • This is useful for users who want to customize or inspect argument properties after creation

Test plan

  • make check passed
  • Documentation builds correctly

🤖 Generated withClaude Code


📚 Documentation preview 📚:https://cpython-previews--144452.org.readthedocs.build/

Add documentation noting that ArgumentParser.add_argument() returnsan Action object, which can be used for further customization orinspection of argument properties.Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* deprecated_ - Whether or not use of the argument is deprecated.

The method returns an :class:`Action` object representing the argument.
This object can be used to further customize behavior or to inspect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It cannot be used for customize the behavior. Action objects are immutable afterwards.

@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@kovan
Copy link
Author

I tested this and Action objects are actually mutable, and modifications afteradd_argument() do take effect:

importargparse# Modify help after addingparser=argparse.ArgumentParser()action=parser.add_argument('--foo',help='Original help')action.help='Modified help'parser.print_help()# Shows: --foo FOO   Modified help# Modify default after addingparser2=argparse.ArgumentParser()action2=parser2.add_argument('--bar',default=10)action2.default=99args=parser2.parse_args([])print(args.bar)# Output: 99# Modify choices after addingparser3=argparse.ArgumentParser()action3=parser3.add_argument('--choice',choices=['a','b'])action3.choices= ['a','b','c']args3=parser3.parse_args(['--choice','c'])# Works!# Modify required after addingparser4=argparse.ArgumentParser()action4=parser4.add_argument('--opt')action4.required=Trueparser4.parse_args([])# Raises error: --opt is required

All modifications (help,default,choices,required) take effect after the Action is added to the parser. Should I keep the original wording, or would you prefer a different phrasing?

@kovan
Copy link
Author

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@picnixz
Copy link
Member

picnixz commentedFeb 3, 2026
edited
Loading

All modifications (help, default, choices, required) take effect after the Action is added to the parser. Should I keep the original wording, or would you prefer a different phrasing?

The question isn't whether it's possible to do it or not, but whether it's meant to be part of the public API or not. Argparse has lots of "public-looking interfaces" but not everything is meant to be public. The attributes may not even be themselves part of the public API (despite them not having underscores). So we should first decide whether Action is public and which part of Action is public.

cc@savannahostrowski

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@savannahostrowskisavannahostrowskiAwaiting requested review from savannahostrowskisavannahostrowski is a code owner

@picnixzpicnixzAwaiting requested review from picnixz

Assignees

No one assigned

Labels

awaiting change reviewdocsDocumentation in the Doc dirskip news

Projects

Status: Todo

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@kovan@picnixz

[8]ページ先頭

©2009-2026 Movatter.jp