Message170312
Okay, this bug is clearly caused by the patch applied for issue 12776. Here's the patch set url: http://hg.python.org/cpython/rev/74f6d87cd471
Now, if you look at this, I think this changes the semantics for non-string default with a type converter, but the question then becomes, what is the intended semantics?
The documentation at:
http://docs.python.org/py3k/library/argparse.html#the-add-argument-method
says:
type - The type to which the command-line argument should be converted.
Okay, that makes perfect sense, because the command line arguments will always be strings in need of conversion. What *doesn't* make sense, IMHO, is that the type conversion should be applied to the default value. This is not documented behavior, but more importantly, is unnecessary, because the .add_argument() call site can just as easily provide a default that's already been converted. However, if you do this with the change, then the default value will get converted twice, once explicitly at the .add_argument() call site, and again, implicitly by argparse.
Also, as this bug report indicates, it then becomes impossible to provide a default that is not converted.
So I believe that the test added for issue 12776 is not correct (specifically test_type_function_call_with_non_string_default), and should be removed. I'm not even sure the original analysis of that bug is correct, but there is a serious semantic ambiguity that needs to be resolved. Specifically, should you support both the use case given in that bug and this bug's use case, and if so, how? E.g. should you even expect this to work:
.add_argument('--foo', type=open, default='/etc/passwd')
?
Maybe another way to look at it is that the conversion should only happen if the action is 'store'. It definitely should not happen if the action is 'append'.
Let's say that the latter is the intended semantics. Attached is a patch that implements these semantics, with a test. If approved, I think we also need to describe the exact semantics in the documentation. I will do that, add a NEWS entry, and back port. |
|
Date |
User |
Action |
Args |
2012-09-11 15:16:32 | barry | set | recipients:
+ barry, georg.brandl, bethard, benjamin.peterson, Arfrever |
2012-09-11 15:16:32 | barry | set | messageid: <1347376592.28.0.212655487579.issue15906@psf.upfronthosting.co.za> |
2012-09-11 15:16:31 | barry | link | issue15906 messages |
2012-09-11 15:16:30 | barry | create | |
|