[New-bugs-announce] [issue12776] argparse: type conversion function should be called only once

Arnaud Fontaine report at bugs.python.org
Thu Aug 18 10:57:58 CEST 2011


New submission from Arnaud Fontaine <arnau at debian.org>:

When specifying a function to be called in type keyword argument of add_argument(), the function is actually called twice (when a default value is set and then when the argument is given).

While this may not be a problem in most cases (such as converting to an int for example), it is an issue for example when trying to open a file whose filename is given as a default value but is not accessible for whatever reason because the first call will fail whereas only the second should be done. I know this may sound like a twisted example but the type function should not be called twice anyhow IMHO.

I tested with Python 2.7 and 3.2 from Debian packages only but the bug seems to be present in py3k and 2.7 hg branches as well.

I have attached a small script showing the issue and two patches (for 2.7 and tip (py3k) hg branches), including an additional test case. All argparse tests pass well with 2.7 and 3.2. Hope that's ok.

----------
components: Library (Lib)
files: example-argparse-type-function-called-twice.py
messages: 142306
nosy: arnau
priority: normal
severity: normal
status: open
title: argparse: type conversion function should be called only once
type: behavior
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file22926/example-argparse-type-function-called-twice.py

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


More information about the New-bugs-announce mailing list