classification
Title: argparse does not honor default argument for nargs=argparse.REMAINDER argument
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bethard, paul.j3, rgov
Priority: normal Keywords:

Created on 2018-12-14 15:59 by rgov, last changed 2018-12-26 22:49 by paul.j3.

Messages (2)
msg331837 - (view) Author: Ryan Govostes (rgov) Date: 2018-12-14 15:59
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('things', nargs=argparse.REMAINDER, default=['nothing'])
parser.parse_args([])
>>> Namespace(things=[])

Since there were no unparsed arguments remaining, the `default` setting for `things` should have been honored. However it silently ignores this setting.

If there's a reason why this wouldn't be desirable, it should raise an exception that the options aren't compatible.
msg332567 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-12-26 22:49
argparse.REMAINDER matches an empty list of arguments, just like '?' and '*'.  So they are always 'filled', even by `parse_args([])`.

'?' and '*' have some special handling of defaults in this case, see in

    argparse.ArgumentParser._get_values

the two 

    value = action.default

REMAINDER has its own section in the function that does nothing with the default.

I think it should be left as is.
History
Date User Action Args
2018-12-26 22:49:30paul.j3setnosy: + paul.j3
messages: + msg332567
2018-12-14 21:57:43terry.reedysetnosy: + bethard
stage: test needed

versions: + Python 3.8, - Python 3.4, Python 3.5
2018-12-14 16:00:12rgovsetversions: + Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7
2018-12-14 15:59:40rgovcreate