New submission from Aschwin <asch...@vanderwoude.info>:

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(
    "--user", 
    dest="user",
    type=lambda x: login2account.get(x, x),
    choices=list(login2account.values()), 
    help="Login name of the user",
)


When using an unknown user, the output is something alike:
   action add: error: argument --assignee: invalid choice: karen (choose from 
<Account(login=john)>, <Account(login=peter)>)

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

----------
components: Library (Lib)
messages: 379856
nosy: avdwoude
priority: normal
severity: normal
status: open
title: lib.argparse._check_value() using repre instead of str
type: enhancement
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42191>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to