[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 2c050d4bc2)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-10-02 16:31:20 +02:00 committed by GitHub
parent cceb25c503
commit 6660d29745
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5069,14 +5069,14 @@ class TestHelpUsageLongSubparserCommand(TestCase):
class TestInvalidArgumentConstructors(TestCase): class TestInvalidArgumentConstructors(TestCase):
"""Test a bunch of invalid Argument constructors""" """Test a bunch of invalid Argument constructors"""
def assertTypeError(self, *args, **kwargs): def assertTypeError(self, *args, errmsg=None, **kwargs):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
self.assertRaises(TypeError, parser.add_argument, self.assertRaisesRegex(TypeError, errmsg, parser.add_argument,
*args, **kwargs) *args, **kwargs)
def assertValueError(self, *args, **kwargs): def assertValueError(self, *args, errmsg=None, **kwargs):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
self.assertRaises(ValueError, parser.add_argument, self.assertRaisesRegex(ValueError, errmsg, parser.add_argument,
*args, **kwargs) *args, **kwargs)
def test_invalid_keyword_arguments(self): def test_invalid_keyword_arguments(self):
@ -5087,7 +5087,8 @@ class TestInvalidArgumentConstructors(TestCase):
def test_missing_destination(self): def test_missing_destination(self):
self.assertTypeError() self.assertTypeError()
for action in ['append', 'store']: for action in ['store', 'append', 'extend']:
with self.subTest(action=action):
self.assertTypeError(action=action) self.assertTypeError(action=action)
def test_invalid_option_strings(self): def test_invalid_option_strings(self):
@ -5102,10 +5103,8 @@ class TestInvalidArgumentConstructors(TestCase):
self.assertValueError('-x', action='foo') self.assertValueError('-x', action='foo')
self.assertValueError('foo', action='baz') self.assertValueError('foo', action='baz')
self.assertValueError('--foo', action=('store', 'append')) self.assertValueError('--foo', action=('store', 'append'))
parser = argparse.ArgumentParser() self.assertValueError('--foo', action="store-true",
with self.assertRaises(ValueError) as cm: errmsg='unknown action')
parser.add_argument("--foo", action="store-true")
self.assertIn('unknown action', str(cm.exception))
def test_multiple_dest(self): def test_multiple_dest(self):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -5118,14 +5117,19 @@ class TestInvalidArgumentConstructors(TestCase):
def test_no_argument_actions(self): def test_no_argument_actions(self):
for action in ['store_const', 'store_true', 'store_false', for action in ['store_const', 'store_true', 'store_false',
'append_const', 'count']: 'append_const', 'count']:
with self.subTest(action=action):
for attrs in [dict(type=int), dict(nargs='+'), for attrs in [dict(type=int), dict(nargs='+'),
dict(choices=['a', 'b'])]: dict(choices=['a', 'b'])]:
with self.subTest(attrs=attrs):
self.assertTypeError('-x', action=action, **attrs) self.assertTypeError('-x', action=action, **attrs)
self.assertTypeError('x', action=action, **attrs)
self.assertTypeError('-x', action=action, nargs=0)
self.assertTypeError('x', action=action, nargs=0)
def test_no_argument_no_const_actions(self): def test_no_argument_no_const_actions(self):
# options with zero arguments # options with zero arguments
for action in ['store_true', 'store_false', 'count']: for action in ['store_true', 'store_false', 'count']:
with self.subTest(action=action):
# const is always disallowed # const is always disallowed
self.assertTypeError('-x', const='foo', action=action) self.assertTypeError('-x', const='foo', action=action)
@ -5133,11 +5137,14 @@ class TestInvalidArgumentConstructors(TestCase):
self.assertTypeError('-x', nargs='*', action=action) self.assertTypeError('-x', nargs='*', action=action)
def test_more_than_one_argument_actions(self): def test_more_than_one_argument_actions(self):
for action in ['store', 'append']: for action in ['store', 'append', 'extend']:
with self.subTest(action=action):
# nargs=0 is disallowed # nargs=0 is disallowed
self.assertValueError('-x', nargs=0, action=action) action_name = 'append' if action == 'extend' else action
self.assertValueError('spam', nargs=0, action=action) self.assertValueError('-x', nargs=0, action=action,
errmsg=f'nargs for {action_name} actions must be != 0')
self.assertValueError('spam', nargs=0, action=action,
errmsg=f'nargs for {action_name} actions must be != 0')
# const is disallowed with non-optional arguments # const is disallowed with non-optional arguments
for nargs in [1, '*', '+']: for nargs in [1, '*', '+']:
@ -5148,7 +5155,7 @@ class TestInvalidArgumentConstructors(TestCase):
def test_required_const_actions(self): def test_required_const_actions(self):
for action in ['store_const', 'append_const']: for action in ['store_const', 'append_const']:
with self.subTest(action=action):
# nargs is always disallowed # nargs is always disallowed
self.assertTypeError('-x', nargs='+', action=action) self.assertTypeError('-x', nargs='+', action=action)