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

Skip to content

Commit 63755f3

Browse files
committed
merge #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.
2 parents 37a0170 + b522828 commit 63755f3

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
@@ -1709,9 +1709,12 @@ def parse_args(self, args=None, namespace=None):
17091709
return args
17101710

17111711
def parse_known_args(self, args=None, namespace=None):
1712-
# args default to the system args
17131712
if args is None:
1713+
# args default to the system args
17141714
args = _sys.argv[1:]
1715+
else:
1716+
# make sure that args are mutable
1717+
args = list(args)
17151718

17161719
# default Namespace built from parser defaults
17171720
if namespace is None:

Lib/test/test_argparse.py

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

46144614
class TestParseKnownArgs(TestCase):
46154615

4616+
def test_arguments_tuple(self):
4617+
parser = argparse.ArgumentParser()
4618+
parser.parse_args(())
4619+
4620+
def test_arguments_list(self):
4621+
parser = argparse.ArgumentParser()
4622+
parser.parse_args([])
4623+
4624+
def test_arguments_tuple_positional(self):
4625+
parser = argparse.ArgumentParser()
4626+
parser.add_argument('x')
4627+
parser.parse_args(('x',))
4628+
4629+
def test_arguments_list_positional(self):
4630+
parser = argparse.ArgumentParser()
4631+
parser.add_argument('x')
4632+
parser.parse_args(['x'])
4633+
46164634
def test_optionals(self):
46174635
parser = argparse.ArgumentParser()
46184636
parser.add_argument('--foo')

0 commit comments

Comments
 (0)