Barry A. Warsaw added the comment:

On Sep 12, 2012, at 04:22 AM, Chris Jerdonek wrote:

>The argparse documentation makes it pretty clear that 'type' is meant to be
>applied only to strings.

Then test_type_function_call_with_non_string_default() which was added to fix
#12776 and #11839 is a bogus test, because it converts default=0 to
'foo_converted'.  This is the test that fails if you restore the
isinstance(action.default, str) test.

>Also, the parse_args() documentation says, "Convert argument strings to
>objects and assign them as attributes of the namespace," but it doesn't say
>anything about also converting non-string defaults.
>
>Thirdly, the documentation for the "default" keyword argument says, "The
>default keyword argument of add_argument(), whose value defaults to None,
>specifies what value should be used if the command-line argument is not
>present."  It doesn't say that the value should be converted before being
>used.

In which case, doing *any* conversion of default seems wrong.  Meaning, you
can't expect the following to work:

p.add_argument('--file', type=open, default='/etc/passwd')
a = p.parse_args([])
a.file.read()

because no --file argument was given, and a.file will be a string.  This
implies that if the command line argument is not given, then user code must
test the type of a.file, and explicitly open it if it's a string, because it
will only be a file object if --file *was* given on the command line.

Then why use type=open at all?  You're better off always expecting a.file to
be a string and do the conversion explicitly after you've parsed the
arguments.  But maybe that's the right interpretation given the documentation.

However, the original fix for #12776 and #11839 does not follow those
semantics.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15906>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to