@@ -5069,15 +5069,15 @@ def custom_formatter(prog):
5069
5069
class TestInvalidArgumentConstructors (TestCase ):
5070
5070
"""Test a bunch of invalid Argument constructors"""
5071
5071
5072
- def assertTypeError (self , * args , ** kwargs ):
5072
+ def assertTypeError (self , * args , errmsg = None , ** kwargs ):
5073
5073
parser = argparse .ArgumentParser ()
5074
- self .assertRaises (TypeError , parser .add_argument ,
5075
- * args , ** kwargs )
5074
+ self .assertRaisesRegex (TypeError , errmsg , parser .add_argument ,
5075
+ * args , ** kwargs )
5076
5076
5077
- def assertValueError (self , * args , ** kwargs ):
5077
+ def assertValueError (self , * args , errmsg = None , ** kwargs ):
5078
5078
parser = argparse .ArgumentParser ()
5079
- self .assertRaises (ValueError , parser .add_argument ,
5080
- * args , ** kwargs )
5079
+ self .assertRaisesRegex (ValueError , errmsg , parser .add_argument ,
5080
+ * args , ** kwargs )
5081
5081
5082
5082
def test_invalid_keyword_arguments (self ):
5083
5083
self .assertTypeError ('-x' , bar = None )
@@ -5087,8 +5087,9 @@ def test_invalid_keyword_arguments(self):
5087
5087
5088
5088
def test_missing_destination (self ):
5089
5089
self .assertTypeError ()
5090
- for action in ['append' , 'store' ]:
5091
- self .assertTypeError (action = action )
5090
+ for action in ['store' , 'append' , 'extend' ]:
5091
+ with self .subTest (action = action ):
5092
+ self .assertTypeError (action = action )
5092
5093
5093
5094
def test_invalid_option_strings (self ):
5094
5095
self .assertValueError ('--' )
@@ -5102,10 +5103,8 @@ def test_invalid_action(self):
5102
5103
self .assertValueError ('-x' , action = 'foo' )
5103
5104
self .assertValueError ('foo' , action = 'baz' )
5104
5105
self .assertValueError ('--foo' , action = ('store' , 'append' ))
5105
- parser = argparse .ArgumentParser ()
5106
- with self .assertRaises (ValueError ) as cm :
5107
- parser .add_argument ("--foo" , action = "store-true" )
5108
- self .assertIn ('unknown action' , str (cm .exception ))
5106
+ self .assertValueError ('--foo' , action = "store-true" ,
5107
+ errmsg = 'unknown action' )
5109
5108
5110
5109
def test_multiple_dest (self ):
5111
5110
parser = argparse .ArgumentParser ()
@@ -5118,39 +5117,47 @@ def test_multiple_dest(self):
5118
5117
def test_no_argument_actions (self ):
5119
5118
for action in ['store_const' , 'store_true' , 'store_false' ,
5120
5119
'append_const' , 'count' ]:
5121
- for attrs in [dict (type = int ), dict (nargs = '+' ),
5122
- dict (choices = ['a' , 'b' ])]:
5123
- self .assertTypeError ('-x' , action = action , ** attrs )
5120
+ with self .subTest (action = action ):
5121
+ for attrs in [dict (type = int ), dict (nargs = '+' ),
5122
+ dict (choices = ['a' , 'b' ])]:
5123
+ with self .subTest (attrs = attrs ):
5124
+ self .assertTypeError ('-x' , action = action , ** attrs )
5125
+ self .assertTypeError ('x' , action = action , ** attrs )
5126
+ self .assertTypeError ('-x' , action = action , nargs = 0 )
5127
+ self .assertTypeError ('x' , action = action , nargs = 0 )
5124
5128
5125
5129
def test_no_argument_no_const_actions (self ):
5126
5130
# options with zero arguments
5127
5131
for action in ['store_true' , 'store_false' , 'count' ]:
5132
+ with self .subTest (action = action ):
5133
+ # const is always disallowed
5134
+ self .assertTypeError ('-x' , const = 'foo' , action = action )
5128
5135
5129
- # const is always disallowed
5130
- self .assertTypeError ('-x' , const = 'foo' , action = action )
5131
-
5132
- # nargs is always disallowed
5133
- self .assertTypeError ('-x' , nargs = '*' , action = action )
5136
+ # nargs is always disallowed
5137
+ self .assertTypeError ('-x' , nargs = '*' , action = action )
5134
5138
5135
5139
def test_more_than_one_argument_actions (self ):
5136
- for action in ['store' , 'append' ]:
5137
-
5138
- # nargs=0 is disallowed
5139
- self .assertValueError ('-x' , nargs = 0 , action = action )
5140
- self .assertValueError ('spam' , nargs = 0 , action = action )
5141
-
5142
- # const is disallowed with non-optional arguments
5143
- for nargs in [1 , '*' , '+' ]:
5144
- self .assertValueError ('-x' , const = 'foo' ,
5145
- nargs = nargs , action = action )
5146
- self .assertValueError ('spam' , const = 'foo' ,
5147
- nargs = nargs , action = action )
5140
+ for action in ['store' , 'append' , 'extend' ]:
5141
+ with self .subTest (action = action ):
5142
+ # nargs=0 is disallowed
5143
+ action_name = 'append' if action == 'extend' else action
5144
+ self .assertValueError ('-x' , nargs = 0 , action = action ,
5145
+ errmsg = f'nargs for { action_name } actions must be != 0' )
5146
+ self .assertValueError ('spam' , nargs = 0 , action = action ,
5147
+ errmsg = f'nargs for { action_name } actions must be != 0' )
5148
+
5149
+ # const is disallowed with non-optional arguments
5150
+ for nargs in [1 , '*' , '+' ]:
5151
+ self .assertValueError ('-x' , const = 'foo' ,
5152
+ nargs = nargs , action = action )
5153
+ self .assertValueError ('spam' , const = 'foo' ,
5154
+ nargs = nargs , action = action )
5148
5155
5149
5156
def test_required_const_actions (self ):
5150
5157
for action in ['store_const' , 'append_const' ]:
5151
-
5152
- # nargs is always disallowed
5153
- self .assertTypeError ('-x' , nargs = '+' , action = action )
5158
+ with self . subTest ( action = action ):
5159
+ # nargs is always disallowed
5160
+ self .assertTypeError ('-x' , nargs = '+' , action = action )
5154
5161
5155
5162
def test_parsers_action_missing_params (self ):
5156
5163
self .assertTypeError ('command' , action = 'parsers' )
0 commit comments