Skip to content

Commit 6a81c3a

Browse files
gh-85935: Improve tests for invalid arguments in test_argparse
Check also specific error messages.
1 parent 8823690 commit 6a81c3a

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
@@ -5262,15 +5262,15 @@ def custom_formatter(prog):
52625262
class TestInvalidArgumentConstructors(TestCase):
52635263
"""Test a bunch of invalid Argument constructors"""
52645264

5265-
def assertTypeError(self, *args, **kwargs):
5265+
def assertTypeError(self, *args, errmsg=None, **kwargs):
52665266
parser = argparse.ArgumentParser()
5267-
self.assertRaises(TypeError, parser.add_argument,
5268-
*args, **kwargs)
5267+
self.assertRaisesRegex(TypeError, errmsg, parser.add_argument,
5268+
*args, **kwargs)
52695269

5270-
def assertValueError(self, *args, **kwargs):
5270+
def assertValueError(self, *args, errmsg=None, **kwargs):
52715271
parser = argparse.ArgumentParser()
5272-
self.assertRaises(ValueError, parser.add_argument,
5273-
*args, **kwargs)
5272+
self.assertRaisesRegex(ValueError, errmsg, parser.add_argument,
5273+
*args, **kwargs)
52745274

52755275
def test_invalid_keyword_arguments(self):
52765276
self.assertTypeError('-x', bar=None)
@@ -5280,8 +5280,9 @@ def test_invalid_keyword_arguments(self):
52805280

52815281
def test_missing_destination(self):
52825282
self.assertTypeError()
5283-
for action in ['append', 'store']:
5284-
self.assertTypeError(action=action)
5283+
for action in ['store', 'append', 'extend']:
5284+
with self.subTest(action=action):
5285+
self.assertTypeError(action=action)
52855286

52865287
def test_invalid_option_strings(self):
52875288
self.assertValueError('--')
@@ -5298,10 +5299,8 @@ def test_invalid_action(self):
52985299
self.assertValueError('-x', action='foo')
52995300
self.assertValueError('foo', action='baz')
53005301
self.assertValueError('--foo', action=('store', 'append'))
5301-
parser = argparse.ArgumentParser()
5302-
with self.assertRaises(ValueError) as cm:
5303-
parser.add_argument("--foo", action="store-true")
5304-
self.assertIn('unknown action', str(cm.exception))
5302+
self.assertValueError('--foo', action="store-true",
5303+
errmsg='unknown action')
53055304

53065305
def test_multiple_dest(self):
53075306
parser = argparse.ArgumentParser()
@@ -5314,39 +5313,47 @@ def test_multiple_dest(self):
53145313
def test_no_argument_actions(self):
53155314
for action in ['store_const', 'store_true', 'store_false',
53165315
'append_const', 'count']:
5317-
for attrs in [dict(type=int), dict(nargs='+'),
5318-
dict(choices=['a', 'b'])]:
5319-
self.assertTypeError('-x', action=action, **attrs)
5316+
with self.subTest(action=action):
5317+
for attrs in [dict(type=int), dict(nargs='+'),
5318+
dict(choices=['a', 'b'])]:
5319+
with self.subTest(attrs=attrs):
5320+
self.assertTypeError('-x', action=action, **attrs)
5321+
self.assertTypeError('x', action=action, **attrs)
5322+
self.assertTypeError('-x', action=action, nargs=0)
5323+
self.assertTypeError('x', action=action, nargs=0)
53205324

53215325
def test_no_argument_no_const_actions(self):
53225326
# options with zero arguments
53235327
for action in ['store_true', 'store_false', 'count']:
5328+
with self.subTest(action=action):
5329+
# const is always disallowed
5330+
self.assertTypeError('-x', const='foo', action=action)
53245331

5325-
# const is always disallowed
5326-
self.assertTypeError('-x', const='foo', action=action)
5327-
5328-
# nargs is always disallowed
5329-
self.assertTypeError('-x', nargs='*', action=action)
5332+
# nargs is always disallowed
5333+
self.assertTypeError('-x', nargs='*', action=action)
53305334

53315335
def test_more_than_one_argument_actions(self):
5332-
for action in ['store', 'append']:
5333-
5334-
# nargs=0 is disallowed
5335-
self.assertValueError('-x', nargs=0, action=action)
5336-
self.assertValueError('spam', nargs=0, action=action)
5337-
5338-
# const is disallowed with non-optional arguments
5339-
for nargs in [1, '*', '+']:
5340-
self.assertValueError('-x', const='foo',
5341-
nargs=nargs, action=action)
5342-
self.assertValueError('spam', const='foo',
5343-
nargs=nargs, action=action)
5336+
for action in ['store', 'append', 'extend']:
5337+
with self.subTest(action=action):
5338+
# nargs=0 is disallowed
5339+
action_name = 'append' if action == 'extend' else action
5340+
self.assertValueError('-x', nargs=0, action=action,
5341+
errmsg=f'nargs for {action_name} actions must be != 0')
5342+
self.assertValueError('spam', nargs=0, action=action,
5343+
errmsg=f'nargs for {action_name} actions must be != 0')
5344+
5345+
# const is disallowed with non-optional arguments
5346+
for nargs in [1, '*', '+']:
5347+
self.assertValueError('-x', const='foo',
5348+
nargs=nargs, action=action)
5349+
self.assertValueError('spam', const='foo',
5350+
nargs=nargs, action=action)
53445351

53455352
def test_required_const_actions(self):
53465353
for action in ['store_const', 'append_const']:
5347-
5348-
# nargs is always disallowed
5349-
self.assertTypeError('-x', nargs='+', action=action)
5354+
with self.subTest(action=action):
5355+
# nargs is always disallowed
5356+
self.assertTypeError('-x', nargs='+', action=action)
53505357

53515358
def test_parsers_action_missing_params(self):
53525359
self.assertTypeError('command', action='parsers')

0 commit comments

Comments
 (0)