[issue12776] argparse: type conversion function should be called only once

flying sheep report at bugs.python.org
Thu Nov 24 10:54:06 CET 2011


flying sheep <flying-sheep at web.de> added the comment:

this is annoying:

i’m creating a reindentation script that reindents any valid python script. the user can specify if, and how many spaces he/she wants to use per indentation level. `0` or leaving the option out means “one tab per level”.

if the argument is given, appended code works as intended. but in the default case, the code fails for any of the two default values i tried.

i would expect that one of the default values works: either `0`, if the default value *is* converted via the `type` function, or `"\t"` if the default value bypasses it.

it seems that argparse applies the `type` function to the default instantly, and then to the argument (be it the already-converted default or a passed option).

this breaks `type` functions which aren’t reflexive for values from their result set, i.e.: `t(x) = y => t(y) = y` must be true for all `x` that the function can handle

----------
nosy: +flying sheep
Added file: http://bugs.python.org/file23767/argparse_test.py

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


More information about the Python-bugs-list mailing list