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-99749: Add closest choice if exists in Argparser if wrong choice picked#99773

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

Closed
abdulrafey38 wants to merge24 commits intopython:mainfromabdulrafey38:fix-issue-99749
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
24 commits
Select commitHold shift + click to select a range
ef8e4fc
Add closet choice if exists in argparser if wrong choice picked
abdulrafey38Nov 25, 2022
7e8dcf8
📜🤖 Added by blurb_it.
blurb-it[bot]Nov 25, 2022
8c754cd
Fix documentation
abdulrafey38Nov 25, 2022
4fb8ce6
Added EOL :hammer:
abdulrafey38Nov 25, 2022
2f197b3
Test cases updated for argparser
abdulrafey38Nov 25, 2022
940a66e
Fixed typo errors :hammer:
abdulrafey38Nov 25, 2022
badc5ed
Test case fix :hammer:
abdulrafey38Nov 25, 2022
2588ef1
Fixed test cse error msg :hammer:
abdulrafey38Nov 25, 2022
ee05c1e
Fixed error test case assert msg :hammer:
abdulrafey38Nov 25, 2022
4a36406
assertion fix :hammer:
abdulrafey38Nov 25, 2022
fe169bc
assertion test case fix
abdulrafey38Nov 25, 2022
9fe0d95
Test Case fix
abdulrafey38Nov 25, 2022
d12e1c4
Reveet to assertRegex from assertEqual
abdulrafey38Nov 25, 2022
35f0961
Remove unused imports :hammer:
abdulrafey38Nov 25, 2022
087895c
assertion msg fixed :hammer:
abdulrafey38Nov 25, 2022
6eeae5c
test cases fixed :hammer:
abdulrafey38Nov 25, 2022
9965694
assert fix
abdulrafey38Nov 25, 2022
4ef218a
assertion fix
abdulrafey38Nov 25, 2022
e63a01c
revert testing
abdulrafey38Nov 25, 2022
bfc9262
test: hammer:
abdulrafey38Nov 25, 2022
b3b4a9e
fixed
abdulrafey38Nov 25, 2022
b8bc465
PR review changes :hammer:
abdulrafey38Nov 26, 2022
ade070f
test case fix :hammer:
abdulrafey38Nov 26, 2022
3753a0d
test case fixation assertIn
abdulrafey38Nov 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletionsLib/argparse.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@
'ZERO_OR_MORE',
]


import difflib as _difflib

Choose a reason for hiding this comment

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

I think we'd probably want to move the import down into the case where the error is thrown, since this is probably not going to be used very often.

import os as _os
import re as _re
import sys as _sys
Expand DownExpand Up@@ -2541,11 +2541,29 @@ def _get_value(self, action, arg_string):
return result

def _check_value(self, action, value):
if not action.choices and isinstance(action.choices, list):
msg = 'Either add options in choices array or remove it'
raise ArgumentError(action, msg)

# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
try:
closest_choice = _difflib.get_close_matches(value, action.choices, 1)
except TypeError:
closest_choice = []

args = {
'value': value,
'choices': ', '.join(map(repr, action.choices)),
}
if closest_choice:
closest_choice = closest_choice[0]
args['closest'] = closest_choice
msg = _('invalid choice: %(value)r, maybe you meant %(closest)r? '
'(choose from %(choices)s)')
else:
msg = _('invalid choice: %(value)r (choose from %(choices)s)')

raise ArgumentError(action, msg % args)

# =======================
Expand Down
4 changes: 2 additions & 2 deletionsLib/test/test_argparse.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2193,9 +2193,9 @@ def test_wrong_argument_subparsers_no_destination_error(self):
subparsers.add_parser('bar')
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('baz',))
self.assertRegex(
self.assertIn(

Choose a reason for hiding this comment

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

We probably also want some additional test cases here.

"error: argument {foo,bar}: invalid choice: 'baz', maybe you meant 'bar'? (choose from 'foo', 'bar')",

Choose a reason for hiding this comment

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

I'd be interested in others' opinions around the verbiage here. It seems like we are using both "Maybe you meant" and "Did you mean" verbiage in the codebase. Not sure if we have any principle around this.

excinfo.exception.stderr,
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
)

def test_optional_subparsers(self):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Add closest choice if exists in Argparser if wrong choice picked.

[8]ページ先頭

©2009-2025 Movatter.jp