[Python-Dev] Issue 15906; regression in argparse in Python 3.3, 3.2, and 2.7

Barry Warsaw barry at python.org
Tue Sep 11 17:34:30 CEST 2012


Issue 15906 describes a problem with argparse that is breaking lots of code in
Ubuntu.  This is a recent regression caused by the fix for issue 12776, and it
affects Python 2.7, 3.2, and 3.3.

I posted a diff that should fix the problem, but at the heart of it is a
semantic ambiguity in argparse that needs clarification.  This needs to be
cleared up before a proper patch can be applied.  I have submitted a patch for
what *I* think reasonable semantics should be, but let's see what you think.

Issue 12776 tried to fix a problem illustrated by this example:

-----snip snip-----
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--file', type=open, default='/etc/passwd')
args = parser.parse_args()

print(args.file.read())

args = parser.parse_args(['--file', '/etc/group'])
print(args.file.read())
-----snip snip-----

What this code is (IMO, sensibly) trying to do is say that args.file will
always be an open file, regardless of whether the path comes from the default
value or is given on the command line.

The problem is that this breaks some other, also sensible code:

-----snip snip-----
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test", dest="test", type=str,
    default=[], action='append')

args = parser.parse_args(['--test', 'bar'])
args.test.append('baz')

args = parser.parse_args()
args.test.append('baz')
-----snip snip-----

This code is saying, I want to accumulate string arguments into a list,
regardless of whether any arguments are given on the command line.

The fix for issue 12776 broke the last two lines of the second example,
because in the no-command-line-arguments-given case, arg.test is the *string*
"[]" and not the actual empty list object.

It seems to me that the semantics could reasonably be implied to mean that the
type converter should only be applied to the default value when
action='store', as is the default.  Then in the second example, because
action='append', the type conversion would not be applied (it makes no sense
to do so).

I have attached a diff to issue 15906 that implements these semantics.  If you
agree, then I will apply this to all of 3.3, 3.2, and 2.7, which are all
affected by this bug (because the original fix for 12776 was applied to all
three branches).

Georg, I would like to apply this to the 3.3 branch.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120911/dea9e2e5/attachment.pgp>


More information about the Python-Dev mailing list