This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse: typechecks default value too early
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 3.4, 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: bethard, r.david.murray, riccardomurri
Priority: normal Keywords:

Created on 2012-08-31 16:08 by riccardomurri, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg169548 - (view) Author: Riccardo Murri (riccardomurri) Date: 2012-08-31 16:08
The `argparse` module (tested with 2.7, but other versions might be
affected) checks the `default` value of an option too early: if the
default value raises an exception, then command-line parsing stops.

Consider for example the following code:

############### begin sample #####################
import os
import argparse

def existing_filename(path):
    if not os.access(path, os.F_OK | os.R_OK):
        raise argparse.ArgumentTypeError("File '%s' does not exist or cannot be read." % path)
    return path

parser = argparse.ArgumentParser(description='Process a file.')
parser.add_argument('file', type=existing_filename, default='/some/weird/default',
                    help='A file to process.')

args = parser.parse_args()
print ("Will process file '%s' ..." % args.file)
############### end sample #####################

The intention here is that the default value should be used *if and
only if* the script users do not provide their own file to process.
It may happen that the default is invalid, but that should be reported
as an error only if the default is used, i.e., users did not provide
any file name to the script.

What happens instead is that the argparse errors out in any case, even
when `--help` is requested or when a valid file name is passed, as the
following two examples show:

    rmurri@xenia:/tmp$ ./argparse-processes-defaults-too-early.py --help
    Traceback (most recent call last):
      File "./argparse-processes-defaults-too-early.py", line 18, in <module>
        args = parser.parse_args()
      File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
        args, argv = self.parse_known_args(args, namespace)
      File "/usr/lib/python2.7/argparse.py", line 1710, in parse_known_args
        default = self._get_value(action, default)
      File "/usr/lib/python2.7/argparse.py", line 2239, in _get_value
        raise ArgumentError(action, msg)
    argparse.ArgumentError: argument file: File '/some/weird/default' does not exist or cannot be read.

    rmurri@xenia:/tmp$ ./argparse-processes-defaults-too-early.py /tmp/xxx.py 
    Traceback (most recent call last):
      File "./argparse-processes-defaults-too-early.py", line 18, in <module>
        args = parser.parse_args()
      File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
        args, argv = self.parse_known_args(args, namespace)
      File "/usr/lib/python2.7/argparse.py", line 1710, in parse_known_args
        default = self._get_value(action, default)
      File "/usr/lib/python2.7/argparse.py", line 2239, in _get_value
        raise ArgumentError(action, msg)
    argparse.ArgumentError: argument file: File '/some/weird/default' does not exist or cannot be read.

Thanks!
msg169551 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-31 16:17
It is possible this could be considered a feature rather than a bug :)  

Let's see what Steven thinks.
msg169570 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-08-31 17:34
It's a bug, but it's already been reported in Issue 12776. There's a patch there, but someone needs to commit it.
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60036
2012-08-31 17:34:41bethardsetstatus: open -> closed
resolution: duplicate
2012-08-31 17:34:22bethardsetsuperseder: argparse: type conversion function should be called only once
messages: + msg169570
2012-08-31 16:17:59r.david.murraysetnosy: + r.david.murray, bethard

messages: + msg169551
versions: + Python 3.2, Python 3.3, Python 3.4
2012-08-31 16:08:10riccardomurricreate