mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766) (GH-125432)
(cherry picked from commit 66b3922b97
)
Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Co-authored-by: rindeal <dev.rindeal@gmail.com>
This commit is contained in:
parent
20323bf733
commit
21524eec48
3 changed files with 36 additions and 8 deletions
|
@ -588,8 +588,7 @@ class HelpFormatter(object):
|
||||||
if action.metavar is not None:
|
if action.metavar is not None:
|
||||||
result = action.metavar
|
result = action.metavar
|
||||||
elif action.choices is not None:
|
elif action.choices is not None:
|
||||||
choice_strs = [str(choice) for choice in action.choices]
|
result = '{%s}' % ','.join(map(str, action.choices))
|
||||||
result = '{%s}' % ','.join(choice_strs)
|
|
||||||
else:
|
else:
|
||||||
result = default_metavar
|
result = default_metavar
|
||||||
|
|
||||||
|
@ -637,8 +636,7 @@ class HelpFormatter(object):
|
||||||
if hasattr(params[name], '__name__'):
|
if hasattr(params[name], '__name__'):
|
||||||
params[name] = params[name].__name__
|
params[name] = params[name].__name__
|
||||||
if params.get('choices') is not None:
|
if params.get('choices') is not None:
|
||||||
choices_str = ', '.join([str(c) for c in params['choices']])
|
params['choices'] = ', '.join(map(str, params['choices']))
|
||||||
params['choices'] = choices_str
|
|
||||||
return self._get_help_string(action) % params
|
return self._get_help_string(action) % params
|
||||||
|
|
||||||
def _iter_indented_subactions(self, action):
|
def _iter_indented_subactions(self, action):
|
||||||
|
@ -763,7 +761,7 @@ def _get_action_name(argument):
|
||||||
elif argument.dest not in (None, SUPPRESS):
|
elif argument.dest not in (None, SUPPRESS):
|
||||||
return argument.dest
|
return argument.dest
|
||||||
elif argument.choices:
|
elif argument.choices:
|
||||||
return '{' + ','.join(argument.choices) + '}'
|
return '{%s}' % ','.join(map(str, argument.choices))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -2600,8 +2598,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
if isinstance(choices, str):
|
if isinstance(choices, str):
|
||||||
choices = iter(choices)
|
choices = iter(choices)
|
||||||
if value not in choices:
|
if value not in choices:
|
||||||
args = {'value': value,
|
args = {'value': str(value),
|
||||||
'choices': ', '.join(map(repr, action.choices))}
|
'choices': ', '.join(map(str, action.choices))}
|
||||||
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
|
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
|
||||||
raise ArgumentError(action, msg % args)
|
raise ArgumentError(action, msg % args)
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import unittest
|
||||||
import argparse
|
import argparse
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
from enum import StrEnum
|
||||||
from test.support import os_helper
|
from test.support import os_helper
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
@ -1021,6 +1022,34 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TestStrEnumChoices(TestCase):
|
||||||
|
class Color(StrEnum):
|
||||||
|
RED = "red"
|
||||||
|
GREEN = "green"
|
||||||
|
BLUE = "blue"
|
||||||
|
|
||||||
|
def test_parse_enum_value(self):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--color', choices=self.Color)
|
||||||
|
args = parser.parse_args(['--color', 'red'])
|
||||||
|
self.assertEqual(args.color, self.Color.RED)
|
||||||
|
|
||||||
|
def test_help_message_contains_enum_choices(self):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--color', choices=self.Color, help='Choose a color')
|
||||||
|
self.assertIn('[--color {red,green,blue}]', parser.format_usage())
|
||||||
|
self.assertIn(' --color {red,green,blue}', parser.format_help())
|
||||||
|
|
||||||
|
def test_invalid_enum_value_raises_error(self):
|
||||||
|
parser = argparse.ArgumentParser(exit_on_error=False)
|
||||||
|
parser.add_argument('--color', choices=self.Color)
|
||||||
|
self.assertRaisesRegex(
|
||||||
|
argparse.ArgumentError,
|
||||||
|
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
|
||||||
|
parser.parse_args,
|
||||||
|
['--color', 'yellow'],
|
||||||
|
)
|
||||||
|
|
||||||
# ================
|
# ================
|
||||||
# Positional tests
|
# Positional tests
|
||||||
# ================
|
# ================
|
||||||
|
@ -2422,7 +2451,7 @@ class TestAddSubparsers(TestCase):
|
||||||
parser.parse_args(('baz',))
|
parser.parse_args(('baz',))
|
||||||
self.assertRegex(
|
self.assertRegex(
|
||||||
excinfo.exception.stderr,
|
excinfo.exception.stderr,
|
||||||
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
|
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo, bar\)\n$"
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_optional_subparsers(self):
|
def test_optional_subparsers(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Always use :func:`str` to print ``choices`` in :mod:`argparse`.
|
Loading…
Add table
Add a link
Reference in a new issue