gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766)

Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
This commit is contained in:
rindeal 2024-10-14 06:36:53 +00:00 committed by GitHub
parent cfc27bc50f
commit 66b3922b97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 8 deletions

View file

@ -16,6 +16,7 @@ import unittest
import argparse
import warnings
from enum import StrEnum
from test.support import captured_stderr
from test.support import import_helper
from test.support import os_helper
@ -985,6 +986,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
# ================
@ -2485,7 +2514,7 @@ class TestAddSubparsers(TestCase):
parser.parse_args(('baz',))
self.assertRegex(
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):