GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-99749: Add closest choice if exists in Argparser if wrong choice picked by abdulrafey38 · Pull Request #99773 · python/cpython Conversation
|
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
3416d13 to
9965694
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Great feature!
Maybe a way to opt out of this feature should be added?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if closest_choice := closest_choice and closest_choice[0] or None: | ||
| args['closest'] = closest_choice |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| if closest_choice := closest_choice and closest_choice[0] or None: | |
| args['closest'] = closest_choice | |
| if closest_choice: | |
| args['closest'] = closest_choice[0] |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
why not the above one liner?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
the one liner is less readable - at least to me
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| args = {'value': value, | ||
| 'choices': ', '.join(map(repr, action.choices))} | ||
| msg = _('invalid choice: %(value)r (choose from %(choices)s)') | ||
| closest_choice = _difflib.get_close_matches(value, action.choices) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
this might fail if action.choices are not strings
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
argparser does not accept choices other than string, it will throw error even before reaching at this line
reference ==> https://stackoverflow.com/questions/49578928/typeerror-int-object-is-not-subscriptable-when-i-try-to-pass-three-arguments
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The second example for the documentation (https://docs.python.org/3/library/argparse.html#choices)
parser = argparse.ArgumentParser(prog='doors.py') parser.add_argument('door', type=int, choices=range(1, 4)) print(parser.parse_args(['3']))
Also - I would use
| closest_choice = _difflib.get_close_matches(value, action.choices) | |
| closest_choice = _difflib.get_close_matches(value, action.choices, 1) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please, instead of just this
closet_choice = _difflib.get_close_matches(value, action.choices)
Add
if isinstance(value, Iterable) and all(isinstance(option, Sized) for option in action.choices)): closet_choice = _difflib.get_close_matches(value, action.choices, 1) # also n=1 (default 3) else: closet_choice = []
or this (better in my opinion)
try: closet_choice = _difflib.get_close_matches(value, action.choices, 1) except TypeError: closet_choice = []
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Closing this pull request & opening a new one as this PR contains unwanted commits... |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Please, don't create new PRs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok @sobolevn i will open this PR again... and close the new one |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@sobolevn reopened this PR again, please have a review |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Quoting myself:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
@sobolevn no this is not enough now, as we change argparser logic here ==> when we pass a choice which is not in choices array it now throws an exception with a different error message (this error message now includes closest suggestions too if available) & in the test case we are passing ['foo','bar'] in choices array and adding 'baz' in argparser. so instead of showing previous message which is (Isn't |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@sobolevn do we have any update here? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I'm still evaluating whether this should be done or not. Here are a few thoughts:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
How's the evaluation going?
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@encukou i can work on it further like importing on errors only, what u say? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
That work might not be merged, so it's better to wait for the decision on whether this is a good idea. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hey there - thanks for the PR! I'm in the process of triaging existing PRs for argparse and noticed this was still open.
A couple of comments but, this still works as intended.
(If you haven't got the bandwidth, let me know and I'd also be happy to carry the PR forward (with credit to you, of course!))
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ] | ||
|
|
||
|
|
||
| import difflib as _difflib |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| with self.assertRaises(ArgumentParserError) as excinfo: | ||
| parser.parse_args(('baz',)) | ||
| self.assertRegex( | ||
| self.assertIn( |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| parser.parse_args(('baz',)) | ||
| self.assertRegex( | ||
| self.assertIn( | ||
| "error: argument {foo,bar}: invalid choice: 'baz', maybe you meant 'bar'? (choose from 'foo', 'bar')", |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hi @savannahostrowski , Yes you can pick it and if you need any help do let me know. Thanks |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Carrying this forward in #124456 with @abdulrafey38's blessing. Thank you! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.