[issue15906] argparse add_argument() confusing behavior when type=str, default=

Barry A. Warsaw report at bugs.python.org
Wed Sep 12 16:04:30 CEST 2012


Barry A. Warsaw added the comment:

On Sep 12, 2012, at 10:39 AM, R. David Murray wrote:

>diff --git a/Lib/argparse.py b/Lib/argparse.py
>--- a/Lib/argparse.py
>+++ b/Lib/argparse.py
>@@ -1962,7 +1962,8 @@
>                     # only if it was defined already in the namespace
>                     if (action.default is not None and
>                         hasattr(namespace, action.dest) and
>-                        action.default is getattr(namespace, action.dest)):
>+                        action.default is getattr(namespace, action.dest) and
>+                        isinstance(action.default, str)):
>                         setattr(namespace, action.dest,
>                                 self._get_value(action, action.default))

For me, this results in the following failure.

[307/371/1] test_argparse
NS(foo='foo_converted')
Namespace(foo=0)
NS(foo='foo_converted')
Namespace(foo=0)
test test_argparse failed -- Traceback (most recent call last):
  File "/home/barry/projects/python/cpython/Lib/test/test_argparse.py", line 4608, in test_type_function_call_with_non_string_default
    self.assertEqual(NS(foo='foo_converted'), args)
  File "/home/barry/projects/python/cpython/Lib/test/test_argparse.py", line 29, in assertEqual
    super(TestCase, self).assertEqual(obj1, obj2)
AssertionError: NS(foo='foo_converted') != Namespace(foo=0)

This test was added for the issue #12776 and #11839 fix, and it's pretty
obvious why it fails.  In the test, default=0 (i.e. a non-string).

Do you think test_type_function_call_with_non_string_default() is a valid test
of expected semantics?  If not, then the test should be removed, and the
changeset for #12667 and #11839 should be re-evaluated, at least to determine
whether accurate tests of those bugs were applied.

If that test is removed, then the above suggested change can be made to fix
#15906.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15906>
_______________________________________


More information about the Python-bugs-list mailing list