Skip to content

Commit 75defab

Browse files
Just quote everything, since its all a str anyway
1 parent b70ed30 commit 75defab

3 files changed

Lines changed: 4 additions & 9 deletions

File tree

Lib/argparse.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,14 +2757,8 @@ def _check_value(self, action, value):
27572757
choices = iter(choices)
27582758

27592759
if value not in choices:
2760-
def _format_choice(choice):
2761-
# For enum members, use repr of the value, not the enum itself
2762-
if hasattr(choice, 'value'):
2763-
return repr(choice.value)
2764-
return repr(choice)
2765-
27662760
args = {'value': str(value),
2767-
'choices': ', '.join(map(_format_choice, action.choices))}
2761+
'choices': ', '.join(repr(str(choice)) for choice in action.choices)}
27682762
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
27692763

27702764
if self.suggest_on_error and isinstance(value, str):

Lib/test/test_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2468,7 +2468,7 @@ def test_suggestions_choices_mixed_types(self):
24682468
with self.assertRaises(ArgumentParserError) as excinfo:
24692469
parser.parse_args(('3',))
24702470
self.assertIn(
2471-
"error: argument foo: invalid choice: '3' (choose from 1, '2')",
2471+
"error: argument foo: invalid choice: '3' (choose from '1', '2')",
24722472
excinfo.exception.stderr,
24732473
)
24742474

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation. Enum members are handled specially to show just their quoted values.
1+
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.
2+

0 commit comments

Comments
 (0)