@@ -5293,15 +5293,15 @@ def custom_formatter(prog):
52935293class TestInvalidArgumentConstructors (TestCase ):
52945294 """Test a bunch of invalid Argument constructors"""
52955295
5296- def assertTypeError (self , * args , ** kwargs ):
5296+ def assertTypeError (self , * args , errmsg = None , ** kwargs ):
52975297 parser = argparse .ArgumentParser ()
5298- self .assertRaises (TypeError , parser .add_argument ,
5299- * args , ** kwargs )
5298+ self .assertRaisesRegex (TypeError , errmsg , parser .add_argument ,
5299+ * args , ** kwargs )
53005300
5301- def assertValueError (self , * args , ** kwargs ):
5301+ def assertValueError (self , * args , errmsg = None , ** kwargs ):
53025302 parser = argparse .ArgumentParser ()
5303- self .assertRaises (ValueError , parser .add_argument ,
5304- * args , ** kwargs )
5303+ self .assertRaisesRegex (ValueError , errmsg , parser .add_argument ,
5304+ * args , ** kwargs )
53055305
53065306 def test_invalid_keyword_arguments (self ):
53075307 self .assertTypeError ('-x' , bar = None )
@@ -5311,8 +5311,9 @@ def test_invalid_keyword_arguments(self):
53115311
53125312 def test_missing_destination (self ):
53135313 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 )
53165317
53175318 def test_invalid_option_strings (self ):
53185319 self .assertValueError ('--' )
@@ -5329,10 +5330,8 @@ def test_invalid_action(self):
53295330 self .assertValueError ('-x' , action = 'foo' )
53305331 self .assertValueError ('foo' , action = 'baz' )
53315332 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' )
53365335
53375336 def test_multiple_dest (self ):
53385337 parser = argparse .ArgumentParser ()
@@ -5345,39 +5344,47 @@ def test_multiple_dest(self):
53455344 def test_no_argument_actions (self ):
53465345 for action in ['store_const' , 'store_true' , 'store_false' ,
53475346 '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 )
53515355
53525356 def test_no_argument_no_const_actions (self ):
53535357 # options with zero arguments
53545358 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 )
53555362
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 )
53615365
53625366 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 )
53755382
53765383 def test_required_const_actions (self ):
53775384 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 )
53815388
53825389 def test_parsers_action_missing_params (self ):
53835390 self .assertTypeError ('command' , action = 'parsers' )
0 commit comments