@@ -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