Regression in argparse for Python 2.7, 3.2 and 3.3
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Python |
Fix Released
|
Unknown
|
|||
python2.7 (Ubuntu) |
Fix Released
|
Critical
|
Matthias Klose | ||
python3.2 (Ubuntu) |
Fix Released
|
Critical
|
Matthias Klose |
Bug Description
Using the following example:
---
#!/usr/bin/python
import argparse
parser = argparse.
parser.
default=[], action='append')
args = parser.parse_args()
args.test.
---
Then running it with simply "python test.py" (WITHOUT passing --test), you get the following:
Traceback (most recent call last):
File "test.py", line 11, in <module>
args.
AttributeError: 'str' object has no attribute 'append'
My understanding of argparse is that when using both default=[] and action="append", args.test should always be a list, empty if not passing --test or a list containing the --test values if passed. After the recent update, it seems to instead default to an empty string.
This must somehow be related to the type field as for some reason, removing "type=str" fixes the issue though I don't really see why and it's still a clear regression.
affects: | ubuntu → python2.7 (Ubuntu) |
Changed in python: | |
status: | Unknown → New |
Changed in python2.7 (Ubuntu): | |
assignee: | Matthias Klose (doko) → Barry Warsaw (barry) |
Changed in python3.2 (Ubuntu): | |
assignee: | Matthias Klose (doko) → Barry Warsaw (barry) |
Changed in python: | |
status: | New → Fix Released |
Barry actually noticed that it's not an empty string as I had assumed, it's the string "[]".
So it looks like the new version of python now sets the default value to type(default) which in this case would be str([]) explaining that value. It's still different from what we'd get with earlier versions.