Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit21524ee

Browse files
[3.12]gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766) (GH-125432)
(cherry picked from commit66b3922)Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>Co-authored-by: rindeal <dev.rindeal@gmail.com>
1 parent20323bf commit21524ee

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

‎Lib/argparse.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ def _metavar_formatter(self, action, default_metavar):
588588
ifaction.metavarisnotNone:
589589
result=action.metavar
590590
elifaction.choicesisnotNone:
591-
choice_strs= [str(choice)forchoiceinaction.choices]
592-
result='{%s}'%','.join(choice_strs)
591+
result='{%s}'%','.join(map(str,action.choices))
593592
else:
594593
result=default_metavar
595594

@@ -637,8 +636,7 @@ def _expand_help(self, action):
637636
ifhasattr(params[name],'__name__'):
638637
params[name]=params[name].__name__
639638
ifparams.get('choices')isnotNone:
640-
choices_str=', '.join([str(c)forcinparams['choices']])
641-
params['choices']=choices_str
639+
params['choices']=', '.join(map(str,params['choices']))
642640
returnself._get_help_string(action)%params
643641

644642
def_iter_indented_subactions(self,action):
@@ -763,7 +761,7 @@ def _get_action_name(argument):
763761
elifargument.destnotin (None,SUPPRESS):
764762
returnargument.dest
765763
elifargument.choices:
766-
return'{'+','.join(argument.choices)+'}'
764+
return'{%s}'%','.join(map(str,argument.choices))
767765
else:
768766
returnNone
769767

@@ -2600,8 +2598,8 @@ def _check_value(self, action, value):
26002598
ifisinstance(choices,str):
26012599
choices=iter(choices)
26022600
ifvaluenotinchoices:
2603-
args= {'value':value,
2604-
'choices':', '.join(map(repr,action.choices))}
2601+
args= {'value':str(value),
2602+
'choices':', '.join(map(str,action.choices))}
26052603
msg=_('invalid choice: %(value)r (choose from %(choices)s)')
26062604
raiseArgumentError(action,msg%args)
26072605

‎Lib/test/test_argparse.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
importargparse
1616
importwarnings
1717

18+
fromenumimportStrEnum
1819
fromtest.supportimportos_helper
1920
fromunittestimportmock
2021

@@ -1021,6 +1022,34 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
10211022
]
10221023

10231024

1025+
classTestStrEnumChoices(TestCase):
1026+
classColor(StrEnum):
1027+
RED="red"
1028+
GREEN="green"
1029+
BLUE="blue"
1030+
1031+
deftest_parse_enum_value(self):
1032+
parser=argparse.ArgumentParser()
1033+
parser.add_argument('--color',choices=self.Color)
1034+
args=parser.parse_args(['--color','red'])
1035+
self.assertEqual(args.color,self.Color.RED)
1036+
1037+
deftest_help_message_contains_enum_choices(self):
1038+
parser=argparse.ArgumentParser()
1039+
parser.add_argument('--color',choices=self.Color,help='Choose a color')
1040+
self.assertIn('[--color {red,green,blue}]',parser.format_usage())
1041+
self.assertIn(' --color {red,green,blue}',parser.format_help())
1042+
1043+
deftest_invalid_enum_value_raises_error(self):
1044+
parser=argparse.ArgumentParser(exit_on_error=False)
1045+
parser.add_argument('--color',choices=self.Color)
1046+
self.assertRaisesRegex(
1047+
argparse.ArgumentError,
1048+
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
1049+
parser.parse_args,
1050+
['--color','yellow'],
1051+
)
1052+
10241053
# ================
10251054
# Positional tests
10261055
# ================
@@ -2422,7 +2451,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
24222451
parser.parse_args(('baz',))
24232452
self.assertRegex(
24242453
excinfo.exception.stderr,
2425-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from'foo', 'bar'\)\n$"
2454+
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo,bar\)\n$"
24262455
)
24272456

24282457
deftest_optional_subparsers(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always use:func:`str` to print ``choices`` in:mod:`argparse`.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp