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

Skip to content

Commit eaae1b7

Browse files
committed
A follow up for issue #15906: change the test for calling the type conversion
on the action's default, reverting it back to previous behavior. Conversion is only done on string defaults. Add a test for this and another test that ensures such type conversions are only called once.
1 parent 55ad651 commit eaae1b7

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +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
1960+
isinstance(action.default, basestring) and
19611961
hasattr(namespace, action.dest) and
19621962
action.default is getattr(namespace, action.dest)):
19631963
setattr(namespace, action.dest,

Lib/test/test_argparse.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,11 +4500,11 @@ def spam(string_to_convert):
45004500
args = parser.parse_args('--foo spam!'.split())
45014501
self.assertEqual(NS(foo='foo_converted'), args)
45024502

4503-
# ================================================================
4504-
# Check that the type function is called with a non-string default
4505-
# ================================================================
4503+
# ==================================================================
4504+
# Check semantics regarding the default argument and type conversion
4505+
# ==================================================================
45064506

4507-
class TestTypeFunctionCallWithNonStringDefault(TestCase):
4507+
class TestTypeFunctionCalledOnDefault(TestCase):
45084508

45094509
def test_type_function_call_with_non_string_default(self):
45104510
def spam(int_to_convert):
@@ -4514,8 +4514,31 @@ def spam(int_to_convert):
45144514
parser = argparse.ArgumentParser()
45154515
parser.add_argument('--foo', type=spam, default=0)
45164516
args = parser.parse_args([])
4517+
# foo should *not* be converted because its default is not a string.
4518+
self.assertEqual(NS(foo=0), args)
4519+
4520+
def test_type_function_call_with_string_default(self):
4521+
def spam(int_to_convert):
4522+
return 'foo_converted'
4523+
4524+
parser = argparse.ArgumentParser()
4525+
parser.add_argument('--foo', type=spam, default='0')
4526+
args = parser.parse_args([])
4527+
# foo is converted because its default is a string.
45174528
self.assertEqual(NS(foo='foo_converted'), args)
45184529

4530+
def test_no_double_type_conversion_of_default(self):
4531+
def extend(str_to_convert):
4532+
return str_to_convert + '*'
4533+
4534+
parser = argparse.ArgumentParser()
4535+
parser.add_argument('--test', type=extend, default='*')
4536+
args = parser.parse_args([])
4537+
# The test argument will be two stars, one coming from the default
4538+
# value and one coming from the type conversion being called exactly
4539+
# once.
4540+
self.assertEqual(NS(test='**'), args)
4541+
45194542
def test_issue_15906(self):
45204543
# Issue #15906: When action='append', type=str, default=[] are
45214544
# providing, the dest value was the string representation "[]" when it

0 commit comments

Comments
 (0)