[New-bugs-announce] [issue15906] argparse add_argument() confusing behavior when type=str, default=[], action='append'

Barry A. Warsaw report at bugs.python.org
Mon Sep 10 18:35:12 CEST 2012


New submission from Barry A. Warsaw:

Run the following code snippet:

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

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

args = parser.parse_args()

print args.test, repr(args.test)
-----snip snip-----

In Python 3.3 (with the appropriate syntactic changes), args.test is clearly a list.  In Python 2.7 and 3.2, args.test is the string "[]".  I can't tell from reading the docs what the expected value should be, but intuitively, the Python 3.3 behavior makes the most sense, i.e. that args.test is a list, not the string representation of a list!

Removing type=str from add_argument() "fixes" the value, but that doesn't seem right because str is the default type so that should have no effect.

We discovered this when we tried to do args.test.append('foo') and tracebacked because args.test wasn't a list.

Original bug report in Ubuntu: https://launchpad.net/bugs/1048710
but I've tested this with upstream hg head for 2.7, 3.2, and 3.3.  It works in Ubuntu's 3.3rc2 but not in upstream head 3.3rc2+, so from reading Misc/NEWS I think the fix for issue #12776 and issue #11839 might be involved.

----------
messages: 170202
nosy: barry
priority: normal
severity: normal
status: open
title: argparse add_argument() confusing behavior when type=str, default=[], action='append'
versions: Python 2.7, Python 3.2

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


More information about the New-bugs-announce mailing list