This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author barry
Recipients Arfrever, barry, benjamin.peterson, bethard, chris.jerdonek, georg.brandl, python-dev, r.david.murray
Date 2012-09-12.14:14:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <20120912101419.3bcd0e35@limelight.wooz.org>
In-reply-to <1347423748.82.0.977111638704.issue15906@psf.upfronthosting.co.za>
Content
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.
History
Date User Action Args
2012-09-12 14:14:22barrysetrecipients: + barry, georg.brandl, bethard, benjamin.peterson, Arfrever, r.david.murray, chris.jerdonek, python-dev
2012-09-12 14:14:21barrylinkissue15906 messages
2012-09-12 14:14:20barrycreate