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

Skip to content

Commit b522828

Browse files
committed
#15847: allow args to be a tuple in parse_args
This fixes a regression introduced by the fix for issue #13922. Although args is not documented as being allowed to be a tuple, previously this worked and so naturally there are programs in the field that depend on it. Patch by Zbyszek Jędrzejewski-Szmek.
1 parent 96efdd4 commit b522828

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/argparse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,9 +1701,12 @@ def parse_args(self, args=None, namespace=None):
17011701
return args
17021702

17031703
def parse_known_args(self, args=None, namespace=None):
1704-
# args default to the system args
17051704
if args is None:
1705+
# args default to the system args
17061706
args = _sys.argv[1:]
1707+
else:
1708+
# make sure that args are mutable
1709+
args = list(args)
17071710

17081711
# default Namespace built from parser defaults
17091712
if namespace is None:

Lib/test/test_argparse.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,24 @@ def spam(int_to_convert):
45224522

45234523
class TestParseKnownArgs(TestCase):
45244524

4525+
def test_arguments_tuple(self):
4526+
parser = argparse.ArgumentParser()
4527+
parser.parse_args(())
4528+
4529+
def test_arguments_list(self):
4530+
parser = argparse.ArgumentParser()
4531+
parser.parse_args([])
4532+
4533+
def test_arguments_tuple_positional(self):
4534+
parser = argparse.ArgumentParser()
4535+
parser.add_argument('x')
4536+
parser.parse_args(('x',))
4537+
4538+
def test_arguments_list_positional(self):
4539+
parser = argparse.ArgumentParser()
4540+
parser.add_argument('x')
4541+
parser.parse_args(['x'])
4542+
45254543
def test_optionals(self):
45264544
parser = argparse.ArgumentParser()
45274545
parser.add_argument('--foo')

0 commit comments

Comments
 (0)