GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-29298: Fix crash with required subparsers without dest by asottile · Pull Request #3680 · python/cpython

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

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.

Sign up for GitHub

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

Merged
miss-islington merged 4 commits into python:main from asottile:issue29298-required-subparsers-no-dest-typeerror
Jul 23, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
d8bb0bd
bpo-29298: Fix crash with required subparsers without dest
asottile Sep 20, 2017
fd1a8c0
Better demonstrate the error messages produced
asottile Sep 22, 2017
6a09976
Use multiple choices to make output more obvious
asottile Sep 22, 2017
36e8f80
re-add required=True to tests
asottile Jul 20, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 Lib/argparse.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,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) + '}'
Comment thread

@merwok merwok Sep 20, 2017

Copy link
Copy Markdown
Member

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 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile Sep 20, 2017

Copy link
Copy Markdown
Contributor Author

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'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 :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile Sep 22, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

@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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@encukou encukou May 25, 2018

Copy link
Copy Markdown
Member

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.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
else:
return None

Expand Down
24 changes: 24 additions & 0 deletions 24 Lib/test/test_argparse.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,30 @@ def test_required_subparsers_default(self):
ret = parser.parse_args(())
self.assertIsNone(ret.command)

def test_required_subparsers_no_destination_error(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(required=True)
subparsers.add_parser('foo')
subparsers.add_parser('bar')
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(())
self.assertRegex(
excinfo.exception.stderr,
'error: the following arguments are required: {foo,bar}\n$'
Comment thread

@serhiy-storchaka serhiy-storchaka May 11, 2018

Copy link
Copy Markdown
Member

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.

Add a space after comma. Why braces are used?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok May 11, 2018

Copy link
Copy Markdown
Member

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 is a notation for alternatives: it means one of foo or bar is required.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@asottile asottile May 11, 2018

Copy link
Copy Markdown
Contributor Author

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 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}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
)

def test_wrong_argument_subparsers_no_destination_error(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(required=True)
subparsers.add_parser('foo')
subparsers.add_parser('bar')
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('baz',))
self.assertRegex(
excinfo.exception.stderr,
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
Comment thread

@serhiy-storchaka serhiy-storchaka May 11, 2018

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile May 11, 2018

Copy link
Copy Markdown
Contributor Author

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.

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 ...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
)

def test_optional_subparsers(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command', required=False)
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2017-09-20-14-43-03.bpo-29298._78CSN.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``TypeError`` when required subparsers without ``dest`` do not receive
arguments. Patch by Anthony Sottile.
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.