Skip to content

Commit ea2ccb3

Browse files
[3.13] gh-85935: Improve tests for invalid arguments in test_argparse (GH-124891) (GH-124901)
Check also specific error messages. (cherry picked from commit 2c050d4)
1 parent 0e0a2da commit ea2ccb3

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

Lib/test/test_argparse.py

+42-35
Original file line numberDiff line numberDiff line change
@@ -5288,15 +5288,15 @@ def custom_formatter(prog):
52885288
class TestInvalidArgumentConstructors(TestCase):
52895289
"""Test a bunch of invalid Argument constructors"""
52905290

5291-
def assertTypeError(self, *args, **kwargs):
5291+
def assertTypeError(self, *args, errmsg=None, **kwargs):
52925292
parser = argparse.ArgumentParser()
5293-
self.assertRaises(TypeError, parser.add_argument,
5294-
*args, **kwargs)
5293+
self.assertRaisesRegex(TypeError, errmsg, parser.add_argument,
5294+
*args, **kwargs)
52955295

5296-
def assertValueError(self, *args, **kwargs):
5296+
def assertValueError(self, *args, errmsg=None, **kwargs):
52975297
parser = argparse.ArgumentParser()
5298-
self.assertRaises(ValueError, parser.add_argument,
5299-
*args, **kwargs)
5298+
self.assertRaisesRegex(ValueError, errmsg, parser.add_argument,
5299+
*args, **kwargs)
53005300

53015301
def test_invalid_keyword_arguments(self):
53025302
self.assertTypeError('-x', bar=None)
@@ -5306,8 +5306,9 @@ def test_invalid_keyword_arguments(self):
53065306

53075307
def test_missing_destination(self):
53085308
self.assertTypeError()
5309-
for action in ['append', 'store']:
5310-
self.assertTypeError(action=action)
5309+
for action in ['store', 'append', 'extend']:
5310+
with self.subTest(action=action):
5311+
self.assertTypeError(action=action)
53115312

53125313
def test_invalid_option_strings(self):
53135314
self.assertValueError('--')
@@ -5324,10 +5325,8 @@ def test_invalid_action(self):
53245325
self.assertValueError('-x', action='foo')
53255326
self.assertValueError('foo', action='baz')
53265327
self.assertValueError('--foo', action=('store', 'append'))
5327-
parser = argparse.ArgumentParser()
5328-
with self.assertRaises(ValueError) as cm:
5329-
parser.add_argument("--foo", action="store-true")
5330-
self.assertIn('unknown action', str(cm.exception))
5328+
self.assertValueError('--foo', action="store-true",
5329+
errmsg='unknown action')
53315330

53325331
def test_multiple_dest(self):
53335332
parser = argparse.ArgumentParser()
@@ -5340,39 +5339,47 @@ def test_multiple_dest(self):
53405339
def test_no_argument_actions(self):
53415340
for action in ['store_const', 'store_true', 'store_false',
53425341
'append_const', 'count']:
5343-
for attrs in [dict(type=int), dict(nargs='+'),
5344-
dict(choices=['a', 'b'])]:
5345-
self.assertTypeError('-x', action=action, **attrs)
5342+
with self.subTest(action=action):
5343+
for attrs in [dict(type=int), dict(nargs='+'),
5344+
dict(choices=['a', 'b'])]:
5345+
with self.subTest(attrs=attrs):
5346+
self.assertTypeError('-x', action=action, **attrs)
5347+
self.assertTypeError('x', action=action, **attrs)
5348+
self.assertTypeError('-x', action=action, nargs=0)
5349+
self.assertTypeError('x', action=action, nargs=0)
53465350

53475351
def test_no_argument_no_const_actions(self):
53485352
# options with zero arguments
53495353
for action in ['store_true', 'store_false', 'count']:
5354+
with self.subTest(action=action):
5355+
# const is always disallowed
5356+
self.assertTypeError('-x', const='foo', action=action)
53505357

5351-
# const is always disallowed
5352-
self.assertTypeError('-x', const='foo', action=action)
5353-
5354-
# nargs is always disallowed
5355-
self.assertTypeError('-x', nargs='*', action=action)
5358+
# nargs is always disallowed
5359+
self.assertTypeError('-x', nargs='*', action=action)
53565360

53575361
def test_more_than_one_argument_actions(self):
5358-
for action in ['store', 'append']:
5359-
5360-
# nargs=0 is disallowed
5361-
self.assertValueError('-x', nargs=0, action=action)
5362-
self.assertValueError('spam', nargs=0, action=action)
5363-
5364-
# const is disallowed with non-optional arguments
5365-
for nargs in [1, '*', '+']:
5366-
self.assertValueError('-x', const='foo',
5367-
nargs=nargs, action=action)
5368-
self.assertValueError('spam', const='foo',
5369-
nargs=nargs, action=action)
5362+
for action in ['store', 'append', 'extend']:
5363+
with self.subTest(action=action):
5364+
# nargs=0 is disallowed
5365+
action_name = 'append' if action == 'extend' else action
5366+
self.assertValueError('-x', nargs=0, action=action,
5367+
errmsg=f'nargs for {action_name} actions must be != 0')
5368+
self.assertValueError('spam', nargs=0, action=action,
5369+
errmsg=f'nargs for {action_name} actions must be != 0')
5370+
5371+
# const is disallowed with non-optional arguments
5372+
for nargs in [1, '*', '+']:
5373+
self.assertValueError('-x', const='foo',
5374+
nargs=nargs, action=action)
5375+
self.assertValueError('spam', const='foo',
5376+
nargs=nargs, action=action)
53705377

53715378
def test_required_const_actions(self):
53725379
for action in ['store_const', 'append_const']:
5373-
5374-
# nargs is always disallowed
5375-
self.assertTypeError('-x', nargs='+', action=action)
5380+
with self.subTest(action=action):
5381+
# nargs is always disallowed
5382+
self.assertTypeError('-x', nargs='+', action=action)
53765383

53775384
def test_parsers_action_missing_params(self):
53785385
self.assertTypeError('command', action='parsers')

0 commit comments

Comments
 (0)