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