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: Default value for an argument that is not in the choices list gets accepted
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sworddragon, bethard, paul.j3, r.david.murray, terry.reedy
Priority: normal Keywords:

Created on 2015-08-29 10:26 by Sworddragon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
msg249318 - (view) Author: (Sworddragon) Date: 2015-08-29 10:26
Normally if a value to an argument is passed that is not in the choices list an error like "test.py: error: argument --test: invalid choice: 'c' (choose from 'a', 'b')" is shown. But if the default is set to an invalid choice no error is shown.
msg249325 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-29 17:05
That might be intentional, and in any case fixing it would be backward incompatible.  I think we should just live with it.
msg249784 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-04 16:42
Which module are you asking about? We could ask author of behavior in question as to intention.
msg249789 - (view) Author: (Sworddragon) Date: 2015-09-04 17:05
I was thinking about cases where the default is variable for example a call to platform.machine() while the choices list (and the script itself) might not support all exotic architectures for its use that might be returned now or in a future version of Python from this function. In this case the call to platform.machine() could be wrapped into a function that checks if the returned value is one of the values in the choices list but that would be the same that I would normally expect to be done by the argparse module.
msg249795 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-09-04 17:33
I assume you are talking about the behavior of the argparse 'choices' feature?  That is what my comment was addressed to.  I didn't actually run a test to see if it behaves the way you describe :)
msg249832 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-09-04 20:53
A related issue which Sworddragon found just before starting this issue is

    http://bugs.python.org/issue9625

The handling of defaults in argparse is a bit complicated.  In general it errs on the side of giving the user/developer freedom to set them how ever they want.  That's especially true in the case of non-string defaults.  A default does not have to be something that the user could give.  In particular the default 'default' is None, which can't be parsed from the command line.  The same for boolean values (the default 'type' does not translate string 'False' to boolean 'False'.)

Errors in the defaults are usually caught during program development.  

If the calling code generates the defaults dynamically, it seems reasonable that it should also check that they are valid.  The validity test for choices is a simple 'in' (contains) one.  'choices' can be a list or dictionary (keys), and can even be dynamically generated.

     parser = ....
     choices = <get a list>
     default = <get a value>
     if default not in choices:
         <scream>
     parser.add_argument(..., choices=choices, default=default)
     etc.
msg249843 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-09-04 22:18
OK, I'm going to close this as "not a bug" then, as it seems to be an intentional design decision.
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69144
2015-09-04 22:18:56r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg249843

stage: resolved
2015-09-04 21:59:25terry.reedysetnosy: + bethard
2015-09-04 20:53:20paul.j3setnosy: + paul.j3
messages: + msg249832
2015-09-04 17:33:29r.david.murraysetmessages: + msg249795
2015-09-04 17:05:44Sworddragonsetmessages: + msg249789
2015-09-04 16:42:06terry.reedysetnosy: + terry.reedy
messages: + msg249784
2015-08-29 17:05:38r.david.murraysetnosy: + r.david.murray
messages: + msg249325
2015-08-29 10:26:34Sworddragoncreate