Message170382
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. |
|
Date |
User |
Action |
Args |
2012-09-12 14:14:22 | barry | set | recipients:
+ barry, georg.brandl, bethard, benjamin.peterson, Arfrever, r.david.murray, chris.jerdonek, python-dev |
2012-09-12 14:14:21 | barry | link | issue15906 messages |
2012-09-12 14:14:20 | barry | create | |
|