-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-97848: argparse: Disallow misbehaving actions for positional arguments #98367
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a6da57d
d8be724
3772f00
b43e81c
6644aab
a0e7b90
f07104c
b26fdb4
a4597b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1540,6 +1540,13 @@ def _get_positional_kwargs(self, dest, **kwargs): | |
msg = _("'required' is an invalid argument for positionals") | ||
raise TypeError(msg) | ||
|
||
# make sure 'store_true', 'store_false', or 'store_const' action | ||
# is not specified | ||
action = kwargs.get('action') | ||
if action in ('store_true', 'store_false', 'store_const'): | ||
msg = _("{action} is an invalid action for positionals", {'action': action}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And these error messages should not be internationalized, as they are programming errors, not user input errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review and comments. I will get back to this PR later this week. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I already created a PR with more general solution: #124839. |
||
raise TypeError(msg) | ||
|
||
# mark positional arguments as required if at least one is | ||
# always required | ||
if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Disallow ``store_true``, ``store_false``, and ``store_const`` actions for positional arguments in :mod:`argparse` module. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels inelegant because we're hardcoding the names of individual actions. I'm not familiar enough with argparse to propose an alternative implementation though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JelleZijlstra thank you for the review.
You say the change feels inelegant, because the names of individual actions are hardcoded. Do I understand correctly that you would prefer some named values or enumeration? Or did you mean something else?
I also am not a huge fan of using these strings directly, but this is the interface exposed by the
ArgumentParser
class and as such I think the names are committed to and won't change.Anyway, I am open to all suggestions on how to improve this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that more elegant and general solution would be
registry()
_StoreTrueAction
etc.It can be tested in
add_argument()
, after testing that the action is a callable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, such flag already exists. You can check if
nargs
is 0 to detect actions that do not consume arguments.