[issue9625] argparse: Problem with defaults for variable nargs when using choices

2020-12-21 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee: rhettinger -> 

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2020-11-30 Thread Borhan Hafez


Change by Borhan Hafez :


--
nosy: +zumoshi

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2020-07-29 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Paul, what is your current thinking on this?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2020-07-29 Thread James Corbett


James Corbett  added the comment:

I would love to get this issue resolved; it seems like everyone agrees that 
it's a bug. It came up for me recently: https://bugs.python.org/issue41047. 
Judging from the comments above, the consensus is that the relevant line, 
`self._check_value(action, value)` should either be replaced with something 
like `if isinstance(value, collections.abc.Sequence): for v in value: 
self._check_value(action, v)` or be removed entirely.

I think the line should just be removed. I think it's fair to assume that users 
of `argparse` know what they're doing, so I think they should be allowed to 
pass default values that conflict with `choices`. Also, removing the line makes 
the behavior consistent with the optionals, which don't check whether default 
values are in `choices`. See the below script:

```
import argparse


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--foo", nargs="+", default=[-1], choices=range(10))
parser.add_argument("--bar", nargs="*", default=-1, choices=range(10))
parser.add_argument("pos", nargs="?", default=-1, choices=range(10))
args = parser.parse_args()
print(args)


if __name__ == '__main__':
main()
```

Which yields:
```
$ python argparse_test.py 
Namespace(foo=[-1], bar=-1, pos=-1)
```

--
nosy: +jameshcorbett

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-11-20 Thread Jan Hutař

Jan Hutař  added the comment:

I think there is a same issue with "nargs='+'" - if you are aware of the, 
please ignore me.

$ python3 --version
Python 3.7.5

With "choices=...", there seems to be a problem:


$ cat bbb.py 
#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('choices', nargs='*',
default=['a', 'b', 'c'],
choices=['a', 'b', 'c'])
args = parser.parse_args()

print(args)
$ ./bbb.py 
usage: bbb.py [-h] [{a,b,c} [{a,b,c} ...]]
bbb.py: error: argument choices: invalid choice: ['a', 'b', 'c'] (choose from 
'a', 'b', 'c')
$ ./bbb.py a c
Namespace(choices=['a', 'c'])


but without choices it works:


$ cat bbb.py 
#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('choices', nargs='*',
default=['a', 'b', 'c'])
args = parser.parse_args()

print(args)
$ ./bbb.py 
Namespace(choices=['a', 'b', 'c'])
$ ./bbb.py a c
Namespace(choices=['a', 'c'])


As I said, if this is a known issue, I'm sorry for the noise.

--
nosy: +Jan Hutař

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-11-18 Thread Mihail Milushev


Mihail Milushev  added the comment:

It looks like choices are broken for nargs='*' even without using default:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('test', nargs='*', choices=['foo', 'bar', 'baz'])
_StoreAction(option_strings=[], dest='test', nargs='*', const=None, 
default=None, type=None, choices=['foo', 'bar', 'baz'], help=None, metavar=None)
>>> parser.parse_args([])
usage: [-h] [{foo,bar,baz} [{foo,bar,baz} ...]]
: error: argument test: invalid choice: [] (choose from 'foo', 'bar', 'baz')

--
nosy: +lanzz

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-08-30 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
priority: high -> normal
versions: +Python 3.9 -Python 2.7, Python 3.5, Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-08-29 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee: bethard -> rhettinger
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-07-12 Thread João Eiras

Change by João Eiras :


--
nosy:  -João Eiras

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2019-07-12 Thread João Eiras

João Eiras  added the comment:

Another workaround for who might ever need it. The benefit of this solution 
comparing to a custom type is that argparse will generate the help string 
properly with the choices, and this solution does workaround the place when the 
bug happens:

class Choices(tuple):
# Python bug https://bugs.python.org/issue27227
def __new__(cls, *args, **kwargs):
x = tuple.__new__(cls, *args, **kwargs)
Choices.__init__(x, *args, **kwargs)
return x

def __init__(self, *args, **kwargs):
self.default = []

def __contains__(self, item):
return tuple.__contains__(self, item) or item is self.default

choices = Choices(("value1", "value2", "value3", ...))

parser.add_argument(
...,
nargs="*",
choices=choices,
default=choices.default,
...)

--
nosy: +João Eiras

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2018-10-16 Thread sebix


Change by sebix :


--
nosy: +sebix

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2018-04-26 Thread Freek Dijkstra

Change by Freek Dijkstra :


--
nosy: +macfreek

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2017-01-19 Thread paul j3

paul j3 added the comment:

Recent StackOverFlow question related to this issue

http://stackoverflow.com/questions/41750896/python-argparse-type-inconsistencies-when-combining-choices-nargs-and-def/41751730#41751730

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2017-01-19 Thread paul j3

Changes by paul j3 :


--
priority: normal -> high

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2016-06-12 Thread Berker Peksag

Changes by Berker Peksag :


--
nosy: +berker.peksag
stage: needs patch -> patch review
versions: +Python 3.5, Python 3.6 -Python 3.2

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2015-08-29 Thread Sworddragon

Changes by Sworddragon sworddrag...@aol.com:


--
nosy: +Sworddragon

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2014-05-01 Thread paul j3

Changes by paul j3 ajipa...@gmail.com:


Added file: http://bugs.python.org/file35128/notes.txt

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2014-04-30 Thread paul j3

paul j3 added the comment:

There's a complicating issue - should these default values be passed through 
the type function?

In most cases in `_get_values`, the string first goes through `_get_value`, and 
then to `_check_value`.

For example the 'else:' case:

value = [self._get_value(action, v) for v in arg_strings]
for v in value:
self._check_value(action, v)

The '*' positional case could coded the same way, allowing:

parser.add_argument('foo',
 nargs='*',
 type=int,
 choices=range(5),
 default=['0',1,'2'])

and objecting to 

 default=[6,'7','a'] # out of range string or int or invalid value

This does impose a further constraint on the 'type' function, that it accepts a 
converted value.  e.g. int(1) is as valid as int('1').

But we need to be careful that this case is handled in a way that is consistent 
with other defaults (including the recent change that delayed evaluating 
defaults till the end).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2013-07-08 Thread paul j3

paul j3 added the comment:

The patch I just posted to http://bugs.python.org/issue16468 uses this fix.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2013-07-03 Thread paul j3

paul j3 added the comment:

Change choices='abc' to choices=['a', 'b', 'c'], as discussed in issue 
16977  (use of string choices is a bad example)

--
Added file: http://bugs.python.org/file30762/issue9625_2.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2013-06-27 Thread paul j3

paul j3 added the comment:

I've added 2 more tests, 

one with default='c', which worked before.

one with default=['a','b'], which only works with this change.

http://bugs.python.org/issue16878 is useful reference, since it documents
the differences between nargs=? and nargs=*, and their handling of
their defaults.

--
Added file: http://bugs.python.org/file30709/issue9625_1.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2013-06-26 Thread paul j3

Changes by paul j3 ajipa...@gmail.com:


--
nosy: +paul.j3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2013-06-15 Thread Cédric Krier

Cédric Krier added the comment:

ping

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2012-11-03 Thread Cédric Krier

Cédric Krier added the comment:

Here is a new version of the patch with tests

--
nosy: +ced
Added file: http://bugs.python.org/file27858/issue9625.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2012-07-21 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

I agree that this looks like a bug. I think the fix is something like the 
attached patch, but it needs some tests to make sure that it fixes your problem.

--
keywords: +patch
Added file: http://bugs.python.org/file26471/issue9625.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9625] argparse: Problem with defaults for variable nargs when using choices

2012-01-23 Thread Martin Pengelly-Phillips

Martin Pengelly-Phillips d...@thesociable.net added the comment:

The real issue is that the choices flag does not work with a default flag and * 
nargs.

The following works as expected:
 parser.add_argument('chosen', nargs='*', default=['a'])
 print(parser.parse_args())
Namespace(chosen=['a'])
 print(parser.parse_args(['a', 'b']))
Namespace(chosen=['a', 'b'])

Introducing a choices constraint breaks down when using the defaults:
 parser.add_argument('chosen', nargs='*', default=['a'], choices=['a', 'b'])
 print(parser.parse_args(['a']))
Namespace(chosen=['a'])
 print(parser.parse_args())
error: argument chosen: invalid choice: ['a'] (choose from 'a', 'b')

I would expect instead to have Namespace.chosen populated with the default list 
as before, but the choices constraint check does not validate correctly.

I think that changing the choices constraint logic to iterate over the default 
values if nargs results in a list would be a possible solution.

--
title: argparse: Problem with defaults for variable nargs - argparse: Problem 
with defaults for variable nargs when using choices

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9625
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com