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

Skip to content

Commit d89774e

Browse files
committed
Merge 3.2 fix updates and tests for issue #15906.
2 parents 77c4553 + 2dceb35 commit d89774e

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
@@ -1961,7 +1961,7 @@ def consume_positionals(start_index):
19611961
# twice (which may fail) if the argument was given, but
19621962
# only if it was defined already in the namespace
19631963
if (action.default is not None and
1964-
isinstance(action, _StoreAction) and
1964+
isinstance(action.default, str) and
19651965
hasattr(namespace, action.dest) and
19661966
action.default is getattr(namespace, action.dest)):
19671967
setattr(namespace, action.dest,

Lib/test/test_argparse.py

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

4594-
# ================================================================
4595-
# Check that the type function is called with a non-string default
4596-
# ================================================================
4594+
# ==================================================================
4595+
# Check semantics regarding the default argument and type conversion
4596+
# ==================================================================
45974597

4598-
class TestTypeFunctionCallWithNonStringDefault(TestCase):
4598+
class TestTypeFunctionCalledOnDefault(TestCase):
45994599

46004600
def test_type_function_call_with_non_string_default(self):
46014601
def spam(int_to_convert):
@@ -4605,8 +4605,31 @@ def spam(int_to_convert):
46054605
parser = argparse.ArgumentParser()
46064606
parser.add_argument('--foo', type=spam, default=0)
46074607
args = parser.parse_args([])
4608+
# foo should *not* be converted because its default is not a string.
4609+
self.assertEqual(NS(foo=0), args)
4610+
4611+
def test_type_function_call_with_string_default(self):
4612+
def spam(int_to_convert):
4613+
return 'foo_converted'
4614+
4615+
parser = argparse.ArgumentParser()
4616+
parser.add_argument('--foo', type=spam, default='0')
4617+
args = parser.parse_args([])
4618+
# foo is converted because its default is a string.
46084619
self.assertEqual(NS(foo='foo_converted'), args)
46094620

4621+
def test_no_double_type_conversion_of_default(self):
4622+
def extend(str_to_convert):
4623+
return str_to_convert + '*'
4624+
4625+
parser = argparse.ArgumentParser()
4626+
parser.add_argument('--test', type=extend, default='*')
4627+
args = parser.parse_args([])
4628+
# The test argument will be two stars, one coming from the default
4629+
# value and one coming from the type conversion being called exactly
4630+
# once.
4631+
self.assertEqual(NS(test='**'), args)
4632+
46104633
def test_issue_15906(self):
46114634
# Issue #15906: When action='append', type=str, default=[] are
46124635
# providing, the dest value was the string representation "[]" when it

0 commit comments

Comments
 (0)