[Bug 1048710] Re: Regression in argparse for Python 2.7, 3.2 and 3.3

Clint Byrum clint at fewbar.com
Tue Sep 11 14:12:20 UTC 2012


This seems related, might even be the same, but fails with a different
case/error message:

#-- begin code--
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("--log-file", "-l", default=sys.stderr, type=argparse.FileType('a'))
args = parser.parse_args()
#-- end code--

$ python bin/test-broken.py 
usage: test-broken.py [-h] [--log-file LOG_FILE]
test-broken.py: error: argument --log-file/-l: invalid FileType('a') value: <open file '<stderr>', mode 'w' at 0x7f201a0fc270>

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to python2.7 in Ubuntu.
https://bugs.launchpad.net/bugs/1048710

Title:
  Regression in argparse for Python 2.7, 3.2 and 3.3

Status in Python:
  New
Status in “python2.7” package in Ubuntu:
  Confirmed
Status in “python3.2” package in Ubuntu:
  Confirmed

Bug description:
  Using the following example:
  ---
  #!/usr/bin/python

  import argparse

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

  args = parser.parse_args()

  args.test.append("something")
  ---

  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.test.append("something")
  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.

To manage notifications about this bug go to:
https://bugs.launchpad.net/python/+bug/1048710/+subscriptions




More information about the foundations-bugs mailing list