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: In argparse add_argument() allows the empty choices argument
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: paul.j3, py.user, r.david.murray
Priority: normal Keywords:

Created on 2015-06-12 21:41 by py.user, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg245277 - (view) Author: py.user (py.user) * Date: 2015-06-12 21:41
A script, configuring argparse to have no choices:

#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('foo', choices=[])

args = parser.parse_args()
print(args)


Running it:

[guest@localhost args]$ ./t.py
usage: t.py [-h] {}
t.py: error: too few arguments
[guest@localhost args]$ ./t.py a
usage: t.py [-h] {}
t.py: error: argument foo: invalid choice: 'a' (choose from )
[guest@localhost args]$ ./t.py ""
usage: t.py [-h] {}
t.py: error: argument foo: invalid choice: '' (choose from )
[guest@localhost args]$

[guest@localhost args]$ ./t.py -h
usage: t.py [-h] {}

positional arguments:
  {}

optional arguments:
  -h, --help  show this help message and exit
[guest@localhost args]$


ISTM, it should throw an exception about empty choices rather than show help with empty choices.
msg245278 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-12 21:48
There's no real reason to throw an error here, and to do so would break backward compatibility and, arguably, functionality: someone autogenerating a choices list might depend on the empty list working.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68629
2015-06-18 01:07:54paul.j3setnosy: + paul.j3
2015-06-12 21:48:33r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg245278

resolution: rejected
stage: resolved
2015-06-12 21:41:11py.usercreate