Issue 24956: Default value for an argument that is not in the choices list gets accepted (original) (raw)

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

Messages (7)
msg249318 - (view) Author: (deleted250130) 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: (deleted250130) 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 = default = if default not in choices: 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:20 admin set github: 69144
2015-09-04 22🔞56 r.david.murray set status: open -> closedresolution: not a bugmessages: + stage: resolved
2015-09-04 21:59:25 terry.reedy set nosy: + bethard
2015-09-04 20:53:20 paul.j3 set nosy: + paul.j3messages: +
2015-09-04 17:33:29 r.david.murray set messages: +
2015-09-04 17:05:44 deleted250130 set messages: +
2015-09-04 16:42:06 terry.reedy set nosy: + terry.reedymessages: +
2015-08-29 17:05:38 r.david.murray set nosy: + r.david.murraymessages: +
2015-08-29 10:26:34 deleted250130 create