-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
lib.argparse._check_value() using repr instead of str #86357
Comments
When using a custom type in add_argument(), the error message on an incorrect value uses repr instead of str, which looks ugly. Example code: login2account = {
c.account.login: c.account for c in self.admin.get_accounts()
} action_add_parser.add_argument( When using an unknown user, the output is something alike: The culprit is the following line in lib.argparse._check_value(): args = {'value': value,
'choices': ', '.join(map(repr, action.choices))} Which should be the following for prettier output: args = {'value': value,
'choices': ', '.join(map(str, action.choices))} |
In the Help formatting, choice_strs = [str(choice) for choice in action.choices] The use of The actual parsing test is just a 'in' test.
There is the added complication that the _get_value() function has already applied the type function the argument string, converting it to a compatible object (for testing). Should argparse be changed, or should it be up to the developer to use a type/choices pairing that gives the desired help and error messages? To improve the discussion, I think your 'self.admin' example should be replaced by a self contained type and class. In the spirit of a StackOverFlow question I'd like concrete minimal example that I can test and play with. I don't want to write a pseudo-account class. |
Do you realize that To illustrate consider two ways of using a simple dictionary. import argparse
adict = {'a': [1,2,3], 'b': 'astring'}
parser = argparse.ArgumentParser()
parser.add_argument('-f',
type = lambda x: adict.get(x,x),
choices = list(adict.values())
)
parser.add_argument('-g', choices = adict)
args = parser.parse_args()
print(args)
print(args.f, adict[args.g]) sample runs: valid key:
help:
Error cases:
With -g, we have to perform the dictionary lookup after parsing, but choices, {a,b}, are clear in both the help and error. With -f, both the help (which uses str) and the error, give wrong user choices, the dictionary values rather than the keys. |
Fixes: python#86357 Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Fixes: python#86357 Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Fixes: python#86357 Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Fixes: python#86357 Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
…oices (GH-117766) Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
…y to print choices (pythonGH-117766) (cherry picked from commit 66b3922) Co-authored-by: rindeal <dev.rindeal@gmail.com> Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
…y to print choices (pythonGH-117766) (cherry picked from commit 66b3922) Co-authored-by: rindeal <dev.rindeal@gmail.com> Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
This is a borderline between a bug and new feature. I decided to backport this change. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: