Skip to content

Commit 4b2f9e9

Browse files
committed
- Issue #15906: Fix a regression in argparse caused by the preceding change,
when action='append', type='str' and default=[].
1 parent d8bbde3 commit 4b2f9e9

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/argparse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ def consume_positionals(start_index):
19571957
# twice (which may fail) if the argument was given, but
19581958
# only if it was defined already in the namespace
19591959
if (action.default is not None and
1960+
isinstance(action, _StoreAction) and
19601961
hasattr(namespace, action.dest) and
19611962
action.default is getattr(namespace, action.dest)):
19621963
setattr(namespace, action.dest,

Lib/test/test_argparse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,6 +4516,16 @@ def spam(int_to_convert):
45164516
args = parser.parse_args([])
45174517
self.assertEqual(NS(foo='foo_converted'), args)
45184518

4519+
def test_issue_15906(self):
4520+
# Issue #15906: When action='append', type=str, default=[] are
4521+
# providing, the dest value was the string representation "[]" when it
4522+
# should have been an empty list.
4523+
parser = argparse.ArgumentParser()
4524+
parser.add_argument('--test', dest='test', type=str,
4525+
default=[], action='append')
4526+
args = parser.parse_args([])
4527+
self.assertEqual(args.test, [])
4528+
45194529
# ======================
45204530
# parse_known_args tests
45214531
# ======================

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ Library
139139
especially problematic for the FileType type, as a default file would always
140140
be opened, even if a file argument was specified on the command line.
141141

142+
- Issue #15906: Fix a regression in argparse caused by the preceding change,
143+
when action='append', type='str' and default=[].
144+
142145
- Issue #13370: Ensure that ctypes works on Mac OS X when Python is
143146
compiled using the clang compiler
144147

0 commit comments

Comments
 (0)