@@ -5262,15 +5262,15 @@ def custom_formatter(prog):
5262
5262
class TestInvalidArgumentConstructors (TestCase ):
5263
5263
"""Test a bunch of invalid Argument constructors"""
5264
5264
5265
- def assertTypeError (self , * args , ** kwargs ):
5265
+ def assertTypeError (self , * args , errmsg = None , ** kwargs ):
5266
5266
parser = argparse .ArgumentParser ()
5267
- self .assertRaises (TypeError , parser .add_argument ,
5268
- * args , ** kwargs )
5267
+ self .assertRaisesRegex (TypeError , errmsg , parser .add_argument ,
5268
+ * args , ** kwargs )
5269
5269
5270
- def assertValueError (self , * args , ** kwargs ):
5270
+ def assertValueError (self , * args , errmsg = None , ** kwargs ):
5271
5271
parser = argparse .ArgumentParser ()
5272
- self .assertRaises (ValueError , parser .add_argument ,
5273
- * args , ** kwargs )
5272
+ self .assertRaisesRegex (ValueError , errmsg , parser .add_argument ,
5273
+ * args , ** kwargs )
5274
5274
5275
5275
def test_invalid_keyword_arguments (self ):
5276
5276
self .assertTypeError ('-x' , bar = None )
@@ -5280,8 +5280,9 @@ def test_invalid_keyword_arguments(self):
5280
5280
5281
5281
def test_missing_destination (self ):
5282
5282
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 )
5285
5286
5286
5287
def test_invalid_option_strings (self ):
5287
5288
self .assertValueError ('--' )
@@ -5298,10 +5299,8 @@ def test_invalid_action(self):
5298
5299
self .assertValueError ('-x' , action = 'foo' )
5299
5300
self .assertValueError ('foo' , action = 'baz' )
5300
5301
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' )
5305
5304
5306
5305
def test_multiple_dest (self ):
5307
5306
parser = argparse .ArgumentParser ()
@@ -5314,39 +5313,47 @@ def test_multiple_dest(self):
5314
5313
def test_no_argument_actions (self ):
5315
5314
for action in ['store_const' , 'store_true' , 'store_false' ,
5316
5315
'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 )
5320
5324
5321
5325
def test_no_argument_no_const_actions (self ):
5322
5326
# options with zero arguments
5323
5327
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 )
5324
5331
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 )
5330
5334
5331
5335
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 )
5344
5351
5345
5352
def test_required_const_actions (self ):
5346
5353
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 )
5350
5357
5351
5358
def test_parsers_action_missing_params (self ):
5352
5359
self .assertTypeError ('command' , action = 'parsers' )
0 commit comments