Skip to content

Commit 6d24cb5

Browse files
[3.13] pythongh-85935: Improve tests for invalid arguments in test_argparse (pythonGH-124891)
Check also specific error messages. (cherry picked from commit 2c050d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent e6fb7fa commit 6d24cb5

1 file changed

Lines changed: 42 additions & 35 deletions

File tree

‎Lib/test/test_argparse.py‎

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5028,15 +5028,15 @@ def custom_formatter(prog):
50285028
class TestInvalidArgumentConstructors(TestCase):
50295029
"""Test a bunch of invalid Argument constructors"""
50305030

5031-
def assertTypeError(self, *args, **kwargs):
5031+
def assertTypeError(self, *args, errmsg=None, **kwargs):
50325032
parser = argparse.ArgumentParser()
5033-
self.assertRaises(TypeError, parser.add_argument,
5034-
*args, **kwargs)
5033+
self.assertRaisesRegex(TypeError, errmsg, parser.add_argument,
5034+
*args, **kwargs)
50355035

5036-
def assertValueError(self, *args, **kwargs):
5036+
def assertValueError(self, *args, errmsg=None, **kwargs):
50375037
parser = argparse.ArgumentParser()
5038-
self.assertRaises(ValueError, parser.add_argument,
5039-
*args, **kwargs)
5038+
self.assertRaisesRegex(ValueError, errmsg, parser.add_argument,
5039+
*args, **kwargs)
50405040

50415041
def test_invalid_keyword_arguments(self):
50425042
self.assertTypeError('-x', bar=None)
@@ -5046,8 +5046,9 @@ def test_invalid_keyword_arguments(self):
50465046

50475047
def test_missing_destination(self):
50485048
self.assertTypeError()
5049-
for action in ['append', 'store']:
5050-
self.assertTypeError(action=action)
5049+
for action in ['store', 'append', 'extend']:
5050+
with self.subTest(action=action):
5051+
self.assertTypeError(action=action)
50515052

50525053
def test_invalid_option_strings(self):
50535054
self.assertValueError('--')
@@ -5064,10 +5065,8 @@ def test_invalid_action(self):
50645065
self.assertValueError('-x', action='foo')
50655066
self.assertValueError('foo', action='baz')
50665067
self.assertValueError('--foo', action=('store', 'append'))
5067-
parser = argparse.ArgumentParser()
5068-
with self.assertRaises(ValueError) as cm:
5069-
parser.add_argument("--foo", action="store-true")
5070-
self.assertIn('unknown action', str(cm.exception))
5068+
self.assertValueError('--foo', action="store-true",
5069+
errmsg='unknown action')
50715070

50725071
def test_multiple_dest(self):
50735072
parser = argparse.ArgumentParser()
@@ -5080,39 +5079,47 @@ def test_multiple_dest(self):
50805079
def test_no_argument_actions(self):
50815080
for action in ['store_const', 'store_true', 'store_false',
50825081
'append_const', 'count']:
5083-
for attrs in [dict(type=int), dict(nargs='+'),
5084-
dict(choices='ab')]:
5085-
self.assertTypeError('-x', action=action, **attrs)
5082+
with self.subTest(action=action):
5083+
for attrs in [dict(type=int), dict(nargs='+'),
5084+
dict(choices=['a', 'b'])]:
5085+
with self.subTest(attrs=attrs):
5086+
self.assertTypeError('-x', action=action, **attrs)
5087+
self.assertTypeError('x', action=action, **attrs)
5088+
self.assertTypeError('-x', action=action, nargs=0)
5089+
self.assertTypeError('x', action=action, nargs=0)
50865090

50875091
def test_no_argument_no_const_actions(self):
50885092
# options with zero arguments
50895093
for action in ['store_true', 'store_false', 'count']:
5094+
with self.subTest(action=action):
5095+
# const is always disallowed
5096+
self.assertTypeError('-x', const='foo', action=action)
50905097

5091-
# const is always disallowed
5092-
self.assertTypeError('-x', const='foo', action=action)
5093-
5094-
# nargs is always disallowed
5095-
self.assertTypeError('-x', nargs='*', action=action)
5098+
# nargs is always disallowed
5099+
self.assertTypeError('-x', nargs='*', action=action)
50965100

50975101
def test_more_than_one_argument_actions(self):
5098-
for action in ['store', 'append']:
5099-
5100-
# nargs=0 is disallowed
5101-
self.assertValueError('-x', nargs=0, action=action)
5102-
self.assertValueError('spam', nargs=0, action=action)
5103-
5104-
# const is disallowed with non-optional arguments
5105-
for nargs in [1, '*', '+']:
5106-
self.assertValueError('-x', const='foo',
5107-
nargs=nargs, action=action)
5108-
self.assertValueError('spam', const='foo',
5109-
nargs=nargs, action=action)
5102+
for action in ['store', 'append', 'extend']:
5103+
with self.subTest(action=action):
5104+
# nargs=0 is disallowed
5105+
action_name = 'append' if action == 'extend' else action
5106+
self.assertValueError('-x', nargs=0, action=action,
5107+
errmsg=f'nargs for {action_name} actions must be != 0')
5108+
self.assertValueError('spam', nargs=0, action=action,
5109+
errmsg=f'nargs for {action_name} actions must be != 0')
5110+
5111+
# const is disallowed with non-optional arguments
5112+
for nargs in [1, '*', '+']:
5113+
self.assertValueError('-x', const='foo',
5114+
nargs=nargs, action=action)
5115+
self.assertValueError('spam', const='foo',
5116+
nargs=nargs, action=action)
51105117

51115118
def test_required_const_actions(self):
51125119
for action in ['store_const', 'append_const']:
5113-
5114-
# nargs is always disallowed
5115-
self.assertTypeError('-x', nargs='+', action=action)
5120+
with self.subTest(action=action):
5121+
# nargs is always disallowed
5122+
self.assertTypeError('-x', nargs='+', action=action)
51165123

51175124
def test_parsers_action_missing_params(self):
51185125
self.assertTypeError('command', action='parsers')

0 commit comments

Comments
 (0)