8000 [3.12] gh-85935: Improve tests for invalid arguments in test_argparse… · python/cpython@6660d29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6660d29

Browse files
[3.12] gh-85935: Improve tests for invalid arguments in test_argparse (GH-124891) (GH-124898)
Check also specific error messages. (cherry picked from commit 2c050d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent cceb25c commit 6660d29

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

Lib/test/test_argparse.py

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

5072-
def assertTypeError(self, *args, **kwargs):
5072+
def assertTypeError(self, *args, errmsg=None, **kwargs):
50735073
parser = argparse.ArgumentParser()
5074-
self.assertRaises(TypeError, parser.add_argument,
5075-
*args, **kwargs)
5074+
self.assertRaisesRegex(TypeError, errmsg, parser.add_argument,
5075+
*args, **kwargs)
50765076

5077-
def assertValueError(self, *args, **kwargs):
5077+
def assertValueError(self, *args, errmsg=None, **kwargs):
50785078
parser = argparse.ArgumentParser()
5079-
self.assertRaises(ValueError, parser.add_argument,
5080-
*args, **kwargs)
5079+
self.assertRaisesRegex(ValueError, errmsg, parser.add_argument,
5080+
*args, **kwargs)
50815081

50825082
def test_invalid_keyword_arguments(self):
50835083
self.assertTypeError('-x', bar=None)
@@ -5087,8 +5087,9 @@ def test_invalid_keyword_arguments(self):
50875087

50885088
def test_missing_destination(self):
50895089
self.assertTypeError()
5090-
for action in ['append', 'store']:
5091-
self.assertTypeError(action=action)
5090+
for action in ['store', 'append', 'extend']:
5091+
with self.subTest(action=action):
5092+
self.assertTypeError(action=action)
50925093

50935094
def test_invalid_option_strings(self):
50945095
self.assertValueError('--')
@@ -5102,10 +5103,8 @@ def test_invalid_action(self):
51025103
self.assertValueError('-x', action='foo')
51035104
self.assertValueError('foo', action='baz')
51045105
self.assertValueError('--foo', action=('store', 'append'))
5105-
parser = argparse.ArgumentParser()
5106-
with self.assertRaises(ValueError) as cm:
5107-
parser.add_argument("--foo", action="store-true")
5108-
self.assertIn('unknown action', str(cm.exception))
5106+
self.assertValueError('--foo', action="store-true",
5107+
errmsg='unknown action')
51095108

51105109
def test_multiple_dest(self):
51115110
parser = argparse.ArgumentParser()
@@ -5118,39 +5117,47 @@ def test_multiple_dest(self):
51185117
def test_no_argument_actions(self):
51195118
for action in ['store_const', 'store_true', 'store_false',
51205119
'append_const', 'count']:
5121-
for attrs in [dict(type=int), dict(nargs='+'),
5122-
dict(choices=['a', 'b'])]:
5123-
self.assertTypeError('-x', action=action, **attrs)
5120+
with self.subTest(action=action):
5121+
for attrs in [dict(type=int), dict(nargs='+'),
5122+
dict(choices=['a', 'b'])]:
5123+
with self.subTest(attrs=attrs):
5124+
self.assertTypeError('-x', action=action, **attrs)
5125+
self.assertTypeError('x', action=action, **attrs)
5126+
self.assertTypeError('-x', action=action, nargs=0)
5127+
self.assertTypeError('x', action=action, nargs=0)
51245128

51255129
def test_no_argument_no_const_actions(self):
51265130
# options with zero arguments
51275131
for action in ['store_true', 'store_false', 'count']:
5132+
with self.subTest(action=action):
5133+
# const is always disallowed
5134+
self.assertTypeError('-x', const='foo', action=action)
51285135

5129-
# const is always disallowed
5130-
self.assertTypeError('-x', const='foo', action=action)
5131-
5132-
# nargs is always disallowed
5133-
self.assertTypeError('-x', nargs='*', action=action)
5136+
# nargs is always disallowed
5137+
self.assertTypeError('-x', nargs='*', action=action)
51345138

51355139
def test_more_than_one_argument_actions(self):
5136-
for action in ['store', 'append']:
5137-
5138-
# nargs=0 is disallowed
5139-
self.assertValueError('-x', nargs=0, action=action)
5140-
self.assertValueError('spam', nargs=0, action=action)
5141-
5142-
# const is disallowed with non-optional arguments
5143-
for nargs in [1, '*', '+']:
5144-
self.assertValueError('-x', const='foo',
5145-
nargs=nargs, action=action)
5146-
self.assertValueError('spam', const='foo',
5147-
nargs=nargs, action=action)
5140+
for action in ['store', 'append', 'extend']:
5141+
with self.subTest(action=action):
5142+
# nargs=0 is disallowed
5143+
action_name = 'append' if action == 'extend' else action
5144+
self.assertValueError('-x', nargs=0, action=action,
5145+
errmsg=f'nargs for {action_name} actions must be != 0')
5146+
self.assertValueError('spam', nargs=0, action=action,
5147+
errmsg=f'nargs for {action_name} actions must be != 0')
5148+
5149+
# const is disallowed with non-optional arguments
5150+
for nargs in [1, '*', '+']:
5151+
self.assertValueError('-x', const='foo',
5152+
nargs=nargs, action=action)
5153+
self.assertValueError('spam', const='foo',
5154+
nargs=nargs, action=action)
51485155

51495156
def test_required_const_actions(self):
51505157
for action in ['store_const', 'append_const']:
5151-
5152-
# nargs is always disallowed
5153-
self.assertTypeError('-x', nargs='+', action=action)
5158+
with self.subTest(action=action):
5159+
# nargs is always disallowed
5160+
self.assertTypeError('-x', nargs='+', action=action)
51545161

51555162
def test_parsers_action_missing_params(self):
51565163
self.assertTypeError('command', action='parsers')

0 commit comments

Comments
 (0)
0