Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b3e975

Browse files
tmblweedmiss-islington
authored andcommitted
bpo-16970: Adding error message for invalid args (GH-14844)
BPO -16970: Adding error message for invalid args Applied the patch argparse-v2 patch issue 16970, ran patch check and the test suite, test_argparse with 0 errors https://bugs.python.org/issue16970
1 parent 2491134 commit 4b3e975

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

Lib/argparse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ def _format_args(self, action, default_metavar):
593593
elif action.nargs == SUPPRESS:
594594
result = ''
595595
else:
596-
formats = ['%s' for _ in range(action.nargs)]
596+
try:
597+
formats = ['%s' for _ in range(action.nargs)]
598+
except TypeError:
599+
raise ValueError("invalid nargs value") from None
597600
result = ' '.join(formats) % get_metavar(action.nargs)
598601
return result
599602

@@ -850,7 +853,7 @@ def __init__(self,
850853
help=None,
851854
metavar=None):
852855
if nargs == 0:
853-
raise ValueError('nargs for store actions must be > 0; if you '
856+
raise ValueError('nargs for store actions must be != 0; if you '
854857
'have nothing to store, actions such as store '
855858
'true or store const may be more appropriate')
856859
if const is not None and nargs != OPTIONAL:
@@ -942,7 +945,7 @@ def __init__(self,
942945
help=None,
943946
metavar=None):
944947
if nargs == 0:
945-
raise ValueError('nargs for append actions must be > 0; if arg '
948+
raise ValueError('nargs for append actions must be != 0; if arg '
946949
'strings are not supplying the value to append, '
947950
'the append const action may be more appropriate')
948951
if const is not None and nargs != OPTIONAL:

Lib/test/test_argparse.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4263,7 +4263,6 @@ class TestHelpSubparsersWithHelpOrdering(HelpTestCase):
42634263

42644264

42654265
class TestHelpMetavarTypeFormatter(HelpTestCase):
4266-
""""""
42674266

42684267
def custom_type(string):
42694268
return string
@@ -5150,6 +5149,35 @@ def test_nargs_3_metavar_length2(self):
51505149
def test_nargs_3_metavar_length3(self):
51515150
self.do_test_no_exception(nargs=3, metavar=("1", "2", "3"))
51525151

5152+
5153+
class TestInvalidNargs(TestCase):
5154+
5155+
EXPECTED_INVALID_MESSAGE = "invalid nargs value"
5156+
EXPECTED_RANGE_MESSAGE = ("nargs for store actions must be != 0; if you "
5157+
"have nothing to store, actions such as store "
5158+
"true or store const may be more appropriate")
5159+
5160+
def do_test_range_exception(self, nargs):
5161+
parser = argparse.ArgumentParser()
5162+
with self.assertRaises(ValueError) as cm:
5163+
parser.add_argument("--foo", nargs=nargs)
5164+
self.assertEqual(cm.exception.args[0], self.EXPECTED_RANGE_MESSAGE)
5165+
5166+
def do_test_invalid_exception(self, nargs):
5167+
parser = argparse.ArgumentParser()
5168+
with self.assertRaises(ValueError) as cm:
5169+
parser.add_argument("--foo", nargs=nargs)
5170+
self.assertEqual(cm.exception.args[0], self.EXPECTED_INVALID_MESSAGE)
5171+
5172+
# Unit tests for different values of nargs
5173+
5174+
def test_nargs_alphabetic(self):
5175+
self.do_test_invalid_exception(nargs='a')
5176+
self.do_test_invalid_exception(nargs="abcd")
5177+
5178+
def test_nargs_zero(self):
5179+
self.do_test_range_exception(nargs=0)
5180+
51535181
# ============================
51545182
# from argparse import * tests
51555183
# ============================

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,3 +1878,4 @@ Edison Abahurire
18781878
Geoff Shannon
18791879
Batuhan Taskaya
18801880
Aleksandr Balezin
1881+
Robert Leenders
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adding a value error when an invalid value in passed to nargs
2+
Patch by Robert Leenders

0 commit comments

Comments
 (0)