@@ -5293,15 +5293,15 @@ def custom_formatter(prog):
5293
5293
class TestInvalidArgumentConstructors (TestCase ):
5294
5294
"""Test a bunch of invalid Argument constructors"""
5295
5295
5296
- def assertTypeError (self , * args , ** kwargs ):
5296
+ def assertTypeError (self , * args , errmsg = None , ** kwargs ):
5297
5297
parser = argparse .ArgumentParser ()
5298
- self .assertRaises (TypeError , parser .add_argument ,
5299
- * args , ** kwargs )
5298
+ self .assertRaisesRegex (TypeError , errmsg , parser .add_argument ,
5299
+ * args , ** kwargs )
5300
5300
5301
- def assertValueError (self , * args , ** kwargs ):
5301
+ def assertValueError (self , * args , errmsg = None , ** kwargs ):
5302
5302
parser = argparse .ArgumentParser ()
5303
- self .assertRaises (ValueError , parser .add_argument ,
5304
- * args , ** kwargs )
5303
+ self .assertRaisesRegex (ValueError , errmsg , parser .add_argument ,
5304
+ * args , ** kwargs )
5305
5305
5306
5306
def test_invalid_keyword_arguments (self ):
5307
5307
self .assertTypeError ('-x' , bar = None )
@@ -5311,8 +5311,9 @@ def test_invalid_keyword_arguments(self):
5311
5311
5312
5312
def test_missing_destination (self ):
5313
5313
self .assertTypeError ()
5314
- for action in ['append' , 'store' ]:
5315
- self .assertTypeError (action = action )
5314
+ for action in ['store' , 'append' , 'extend' ]:
5315
+ with self .subTest (action = action ):
5316
+ self .assertTypeError (action = action )
5316
5317
5317
5318
def test_invalid_option_strings (self ):
5318
5319
self .assertValueError ('--' )
@@ -5329,10 +5330,8 @@ def test_invalid_action(self):
5329
5330
self .assertValueError ('-x' , action = 'foo' )
5330
5331
self .assertValueError ('foo' , action = 'baz' )
5331
5332
self .assertValueError ('--foo' , action = ('store' , 'append' ))
5332
- parser = argparse .ArgumentParser ()
5333
- with self .assertRaises (ValueError ) as cm :
5334
- parser .add_argument ("--foo" , action = "store-true" )
5335
- self .assertIn ('unknown action' , str (cm .exception ))
5333
+ self .assertValueError ('--foo' , action = "store-true" ,
5334
+ errmsg = 'unknown action' )
5336
5335
5337
5336
def test_multiple_dest (self ):
5338
5337
parser = argparse .ArgumentParser ()
@@ -5345,39 +5344,47 @@ def test_multiple_dest(self):
5345
5344
def test_no_argument_actions (self ):
5346
5345
for action in ['store_const' , 'store_true' , 'store_false' ,
5347
5346
'append_const' , 'count' ]:
5348
- for attrs in [dict (type = int ), dict (nargs = '+' ),
5349
- dict (choices = ['a' , 'b' ])]:
5350
- self .assertTypeError ('-x' , action = action , ** attrs )
5347
+ with self .subTest (action = action ):
5348
+ for attrs in [dict (type = int ), dict (nargs = '+' ),
5349
+ dict (choices = ['a' , 'b' ])]:
5350
+ with self .subTest (attrs = attrs ):
5351
+ self .assertTypeError ('-x' , action = action , ** attrs )
5352
+ self .assertTypeError ('x' , action = action , ** attrs )
5353
+ self .assertTypeError ('-x' , action = action , nargs = 0 )
5354
+ self .assertTypeError ('x' , action = action , nargs = 0 )
5351
5355
5352
5356
def test_no_argument_no_const_actions (self ):
5353
5357
# options with zero arguments
5354
5358
for action in ['store_true' , 'store_false' , 'count' ]:
5359
+ with self .subTest (action = action ):
5360
+ # const is always disallowed
5361
+ self .assertTypeError ('-x' , const = 'foo' , action = action )
5355
5362
5356
- # const is always disallowed
5357
- self .assertTypeError ('-x' , const = 'foo' , action = action )
5358
-
5359
- # nargs is always disallowed
5360
- self .assertTypeError ('-x' , nargs = '*' , action = action )
5363
+ # nargs is always disallowed
5364
+ self .assertTypeError ('-x' , nargs = '*' , action = action )
5361
5365
5362
5366
def test_more_than_one_argument_actions (self ):
5363
- for action in ['store' , 'append' ]:
5364
-
5365
- # nargs=0 is disallowed
5366
- self .assertValueError ('-x' , nargs = 0 , action = action )
5367
- self .assertValueError ('spam' , nargs = 0 , action = action )
5368
-
5369
- # const is disallowed with non-optional arguments
5370
- for nargs in [1 , '*' , '+' ]:
5371
- self .assertValueError ('-x' , const = 'foo' ,
5372
- nargs = nargs , action = action )
5373
- self .assertValueError ('spam' , const = 'foo' ,
5374
- nargs = nargs , action = action )
5367
+ for action in ['store' , 'append' , 'extend' ]:
5368
+ with self .subTest (action = action ):
5369
+ # nargs=0 is disallowed
5370
+ action_name = 'append' if action == 'extend' else action
5371
+ self .assertValueError ('-x' , nargs = 0 , action = action ,
5372
+ errmsg = f'nargs for { action_name } actions must be != 0' )
5373
+ self .assertValueError ('spam' , nargs = 0 , action = action ,
5374
+ errmsg = f'nargs for { action_name } actions must be != 0' )
5375
+
5376
+ # const is disallowed with non-optional arguments
5377
+ for nargs in [1 , '*' , '+' ]:
5378
+ self .assertValueError ('-x' , const = 'foo' ,
5379
+ nargs = nargs , action = action )
5380
+ self .assertValueError ('spam' , const = 'foo' ,
5381
+ nargs = nargs , action = action )
5375
5382
5376
5383
def test_required_const_actions (self ):
5377
5384
for action in ['store_const' , 'append_const' ]:
5378
-
5379
- # nargs is always disallowed
5380
- self .assertTypeError ('-x' , nargs = '+' , action = action )
5385
+ with self . subTest ( action = action ):
5386
+ # nargs is always disallowed
5387
+ self .assertTypeError ('-x' , nargs = '+' , action = action )
5381
5388
5382
5389
def test_parsers_action_missing_params (self ):
5383
5390
self .assertTypeError ('command' , action = 'parsers' )
0 commit comments