classification
Title: When -h is used with argparse, default values that fail should not matter
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: argparse: type conversion function should be called only once
View: 12776
Assigned To: Nosy List: Joshua.Chia, bethard, eric.araujo, eric.smith, petri.lehtinen, tshepang
Priority: normal Keywords:

Created on 2011-10-26 21:25 by Joshua.Chia, last changed 2013-03-31 21:51 by eric.araujo. This issue is now closed.

Files
File name Uploaded Description Edit
test.py Joshua.Chia, 2013-03-31 21:32
Messages (6)
msg146458 - (view) Author: Joshua Chia (Joshua.Chia) Date: 2011-10-26 21:25
What steps will reproduce the problem?
1. Make a script containing this code:

 parser = argparse.ArgumentParser()
 parser.add_argument('-i', '--input-base-directory',
                     type=argparse.FileType('r'),
                     default='/home')
 parser.parse_args()

2. Run the script with '-h'.
3. See it complain that '/home' is a directory

Detailed explanation:
With '-h', we just need to print the help message. We shouldn't fail on a default value that's wrong or else the help message will not get printed and the user will have no idea what the problem is. This is a valid use case as I want to provide a default location, but I can't guarantee that the location works on the user's machine. I end up doing my own checking.
msg146496 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-10-27 15:46
I’d argue this is a behavior bug that could be fixed in 2.7 and 3.2 too, but Steven will decide.
msg146826 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2011-11-02 06:34
Sounds like a but to me, too.
msg149530 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-12-15 11:01
I think http://bugs.python.org/issue12776, 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?

(And yes, I agree this is a bug.)
msg185678 - (view) Author: Joshua Chia (Joshua.Chia) Date: 2013-03-31 21:32
Added test case
msg185680 - (view) Author: Joshua Chia (Joshua.Chia) Date: 2013-03-31 21:34
Seems to be duplicate of http://bugs.python.org/issue12776
History
Date User Action Args
2013-03-31 21:51:07eric.araujosetsuperseder: argparse: type conversion function should be called only once
stage: test needed -> resolved
2013-03-31 21:34:04Joshua.Chiasetmessages: + msg185680
2013-03-31 21:32:55Joshua.Chiasetstatus: open -> closed
files: + test.py
resolution: duplicate
messages: + msg185678
2012-03-18 21:39:46tshepangsetnosy: + tshepang
2011-12-15 11:01:57bethardsetmessages: + msg149530
2011-11-02 06:34:33petri.lehtinensetstage: test needed
messages: + msg146826
versions: + Python 2.7, Python 3.2
2011-10-27 15:46:42eric.araujosetnosy: + eric.araujo

messages: + msg146496
versions: + Python 3.3, - Python 2.7
2011-10-27 12:28:28petri.lehtinensetnosy: + petri.lehtinen
2011-10-27 01:30:53eric.smithsetnosy: + bethard, eric.smith
2011-10-26 21:25:51Joshua.Chiacreate