classification
Title: argparse: unexpected behavior of default for FileType('w')
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Mike.Meyer, Paolo.Elvati, Stefan.Pfeiffer, bethard, manveru, python-dev
Priority: normal Keywords:

Created on 2011-04-12 19:56 by Paolo.Elvati, last changed 2012-09-01 03:16 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
fopatch Mike.Meyer, 2012-05-03 01:16
Messages (6)
msg133598 - (view) Author: Paolo Elvati (Paolo.Elvati) Date: 2011-04-12 19:56
Hi,
when a default is specified for a file argument that is open with writing permission (FileType('w')), the default file is always created even if the argument is specified in the command line. 
For example he code:

import argparse 
parser = argparse.ArgumentParser() 
parser.add_argument( 
"-o", 
default = 'fake', 
dest = 'OutputFile', 
type = argparse.FileType('w') 
) 
args = parser.parse_args() 

will create the empty file "fake" even if the -o option is given. 
The value inside the code of args.Outputfile is not affected.

Paolo
msg134559 - (view) Author: Manveru (manveru) Date: 2011-04-27 12:44
I have the same issue with default here with 2.7.

Fortunately I have my own type function so I can prevent is by changing my internal state. This is however only a workaround for real bug in the argparse.
msg134956 - (view) Author: Stefan Pfeiffer (Stefan.Pfeiffer) Date: 2011-05-02 09:10
Happens in 3.2, too...

Would be nice to see that fixed.

Stefan
msg149546 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-12-15 12:38
I think Issue 12776, which delays type conversions on defaults, should solve this problem, right? If you agree, could you add your code here as a test case to that issue and mark this as duplicate?
msg159828 - (view) Author: Mike Meyer (Mike.Meyer) Date: 2012-05-03 01:16
Steven - 12776 indeed fixes this issue. I applied the patch from it to a build of todays checkout, verified that my simple test script worked, then wrote some test cases for test_argparse.

I've uploaded the patch for that test to both issues. I can't close this as a duplicate, though.
msg169609 - (view) Author: Roundup Robot (python-dev) Date: 2012-09-01 03:16
New changeset 1b614921aefa by R David Murray in branch '3.2':
#12776,#11839: call argparse type function only once.
http://hg.python.org/cpython/rev/1b614921aefa

New changeset 74f6d87cd471 by R David Murray in branch 'default':
Merge #12776,#11839: call argparse type function only once.
http://hg.python.org/cpython/rev/74f6d87cd471

New changeset 62b5667ef2f4 by R David Murray in branch '2.7':
#12776,#11839: call argparse type function only once.
http://hg.python.org/cpython/rev/62b5667ef2f4
History
Date User Action Args
2012-09-01 03:16:48python-devsetnosy: + python-dev
messages: + msg169609
2012-05-03 09:36:44Paolo.Elvatisetstatus: open -> closed
2012-05-03 09:36:13Paolo.Elvatisetstatus: closed -> open
2012-05-03 09:34:01Paolo.Elvatisetstatus: open -> closed
resolution: duplicate
2012-05-03 01:16:46Mike.Meyersetfiles: + fopatch
nosy: + Mike.Meyer
messages: + msg159828

2011-12-15 12:38:19bethardsetmessages: + msg149546
2011-05-02 09:10:18Stefan.Pfeiffersetnosy: + Stefan.Pfeiffer
messages: + msg134956
2011-04-27 12:44:00manverusetnosy: + manveru

messages: + msg134559
versions: + Python 2.7
2011-04-12 19:56:29Paolo.Elvaticreate