Skip to content
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

bpo-29298: Fix crash with required subparsers without dest #3680

Conversation

@asottile
Copy link
Contributor

@asottile asottile commented Sep 20, 2017

https://bugs.python.org/issue29298

Automerge-Triggered-By: GH:encukou

@asottile
Copy link
Contributor Author

@asottile asottile commented Sep 20, 2017

@merwok this is a followup from #3027 (comment)

@@ -704,6 +704,8 @@ def _get_action_name(argument):
return argument.metavar
elif argument.dest not in (None, SUPPRESS):
return argument.dest
elif argument.choices:
return '{' + ','.join(argument.choices) + '}'
Copy link
Member

@merwok merwok Sep 20, 2017

Choose a reason for hiding this comment

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

The test doesn’t really help me understand what this change does (i.e. what a human would see in an error case).

I’m also not sure if some of the changes in the various patches for bpo-9253 (especially in tests) could be useful here.

Copy link
Contributor Author

@asottile asottile Sep 20, 2017

Choose a reason for hiding this comment

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

I'll check those out. I had a difficult time demonstrating (by assertion) the actual output (didn't find any examples) -- I'll bang against that and hopefully I'll make something more expressive :)

Copy link
Contributor Author

@asottile asottile Sep 22, 2017

Choose a reason for hiding this comment

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

@merwok looks like they had the same idea I had (but a different implementation) -- I took inspiration from one of their tests for additional coverage and updated the tests to hopefully be more expressive (and have a better failure mode)

Copy link
Member

@encukou encukou May 25, 2018

Choose a reason for hiding this comment

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

Code for this is already in _metavar_formatter:

        elif action.choices is not None:
            choice_strs = [str(choice) for choice in action.choices]
            result = '{%s}' % ','.join(choice_strs)

The metavar formatter can be called, if the parser is known (as self), using:

            default = self._get_default_metavar_for_positional(action)
            metavar, = self._metavar_formatter(action, default)(1)

Perhaps _get_action_name should be made into a method, or take the parser as an optional argument?

merwok
merwok previously requested changes Sep 22, 2017
Copy link
Member

@merwok merwok left a comment

Thanks a lot! Left comments about the error message UI.

Lib/test/test_argparse.py Outdated Show resolved Hide resolved
Lib/test/test_argparse.py Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Sep 22, 2017

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 I didn't expect the Spanish Inquisition!. 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.

@asottile
Copy link
Contributor Author

@asottile asottile commented Sep 26, 2017

@merwok care to take another look at this?

@asottile
Copy link
Contributor Author

@asottile asottile commented Sep 29, 2017

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Sep 29, 2017

Nobody expects the Spanish Inquisition!

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

@merwok
Copy link
Member

@merwok merwok commented Sep 29, 2017

I haven’t had the time to double-check the error messages, maybe this week-end.

@sn1p3r46
Copy link

@sn1p3r46 sn1p3r46 commented May 9, 2018

Good evening,
do someone has an idea in which future version of python this fix will be deployed?

parser.parse_args(())
self.assertRegex(
excinfo.exception.stderr,
'error: the following arguments are required: {foo,bar}\n$'
Copy link
Member

@serhiy-storchaka serhiy-storchaka May 11, 2018

Choose a reason for hiding this comment

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

Add a space after comma. Why braces are used?

Copy link
Member

@merwok merwok May 11, 2018

Choose a reason for hiding this comment

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

I think this is a notation for alternatives: it means one of foo or bar is required.

Copy link
Contributor Author

@asottile asottile May 11, 2018

Choose a reason for hiding this comment

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

This is consistent with the rendering of choices:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', choices=('baz', 'womp'))
parser.parse_args()
$ python test.py --help
usage: test.py [-h] [--foo {baz,womp}]

optional arguments:
  -h, --help        show this help message and exit
  --foo {baz,womp}

parser.parse_args(('baz',))
self.assertRegex(
excinfo.exception.stderr,
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
Copy link
Member

@serhiy-storchaka serhiy-storchaka May 11, 2018

Choose a reason for hiding this comment

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

"foo, bar" is duplicated. And this error message looks confusing to me. What it means?

Copy link
Contributor Author

@asottile asottile May 11, 2018

Choose a reason for hiding this comment

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

yeah I didn't really know what to do for this, it's essentially an anonymous positional choices argument. Personally I'd love a default dest='command' but I imagine that could cause some incompatibility?

Then this would read something like error: argument command: invalid choice ...

@csabella
Copy link
Contributor

@csabella csabella commented Jan 25, 2020

I have requested reviews of this, but it also looks like @asottile may need to address some questions as well.

@asottile
Copy link
Contributor Author

@asottile asottile commented Jan 26, 2020

I have requested reviews of this, but it also looks like @asottile may need to address some questions as well.

I believe I've addressed everything currently open, unless I'm missing something 🤔

@csabella csabella requested review from merwok, encukou and serhiy-storchaka and removed request for encukou, merwok and serhiy-storchaka Jun 12, 2020
@merwok merwok requested review from merwok and removed request for merwok Jul 20, 2021
@merwok merwok dismissed their stale review Jul 20, 2021

passed review to Petr

@asottile
Copy link
Contributor Author

@asottile asottile commented Jul 20, 2021

I'll trivial-rebase to get a fresh set of builders

@asottile asottile force-pushed the issue29298-required-subparsers-no-dest-typeerror branch from 276e844 to 6a09976 Jul 20, 2021
@miss-islington miss-islington merged commit 17575f7 into python:main Jul 23, 2021
13 checks passed
@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Jul 23, 2021

Thanks @asottile for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.9.
🐍🍒🤖

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Jul 23, 2021

GH-27303 is a backport of this pull request to the 3.10 branch.

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Jul 23, 2021

GH-27304 is a backport of this pull request to the 3.9 branch.

miss-islington added a commit to miss-islington/cpython that referenced this issue Jul 23, 2021
…3680)

Automerge-Triggered-By: GH:encukou
(cherry picked from commit 17575f7)

Co-authored-by: Anthony Sottile <asottile@umich.edu>
miss-islington added a commit to miss-islington/cpython that referenced this issue Jul 23, 2021
…3680)

Automerge-Triggered-By: GH:encukou
(cherry picked from commit 17575f7)

Co-authored-by: Anthony Sottile <asottile@umich.edu>
encukou pushed a commit that referenced this issue Jul 23, 2021
…H-27303)

(cherry picked from commit 17575f7)

Co-authored-by: Anthony Sottile <asottile@umich.edu>
encukou pushed a commit that referenced this issue Jul 23, 2021
…H-27304)

(cherry picked from commit 17575f7)

Co-authored-by: Anthony Sottile <asottile@umich.edu>
@asottile asottile deleted the issue29298-required-subparsers-no-dest-typeerror branch Jul 23, 2021
JuniorJPDJ added a commit to JuniorJPDJ/cpython that referenced this issue Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet