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