-
-
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
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
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.
Thanks you for your proposal!
Technically this is a breaking change. But, in practice I highly doubt that anyone is using it.
Misc/NEWS.d/next/Library/2022-10-17-20-20-18.gh-issue-97848.RnkhKZ.rst
Outdated
Show resolved
Hide resolved
Retriggering CI to fix CLA status. |
…khKZ.rst Co-authored-by: Nikita Sobolev <[email protected]>
Hi, is there anything else I need to complete to get this merged? |
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.
Python is run by volunteers, so reviews by core-devs can take some time :)
But, it is fine to ping folks once in a while if there's no activity.
I get it and did not mean to be rude or impatient, I'm just keen to add a tiny contribution :-) Thank you for the reviews! |
@sobolevn Since you have been kind enough to review this and provide valuable cues, could you please have a look once more? |
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 this makes sense for 3.12
I will ping @JelleZijlstra as well :)
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 I'll leave this for another core dev.
@@ -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 |
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
- either add an optional boolean parameter for
registry()
- or add an optional class attribute in
_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.
@rhettinger Could you please have a look and review this PR? @JelleZijlstra did have a look and decided to pass it some other core dev, but has not assigned anyone else. Thanks. |
@@ -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 |
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
- either add an optional boolean parameter for
registry()
- or add an optional class attribute in
_StoreTrueAction
etc.
It can be tested in add_argument()
, after testing that the action is a callable.
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The argument of _()
should be a string, not a tuple. I afraid that your tests are passed because a wrong TypeError is raised. You should test also the error message to be sure that this is the correct error.
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I already created a PR with more general solution: #124839.
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 phrase |
Issue #97848
The disallowed actions are:
'store_true'
,'store_false'
and'store_const'
, which cause a counterintuitive behaviour.Tests for new behaviour are included.
store_true
,store_false
, orstore_const
action #97848