[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 
<https://bugs.python.org/issue9625>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41436] BUG a simple "and" and "or"

2020-07-29 Thread James Corbett


James Corbett  added the comment:

I think this would have been a better fit for a StackOverflow issue: 
https://stackoverflow.com/questions/tagged/python. Also, it's not a compilation 
error and it doesn't have anything to do with CPython's testing framework. 

Anyway, I don't think this is a bug. For a string `ch`, it is always true that 
either `ch != 'n'` or `ch != 'N'`---no string is equal to both `'N'` and `'n'`. 
Therefore your `while` condition will always be true and the loop will always 
continue.

As you already noted, your loop will terminate properly if you used `and`. You 
could also rewrite it as `while ch not in ('n', 'N'):` which I think is clearer.

--
nosy: +jameshcorbett, xtreak
type: compile error -> behavior

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



[issue41430] Document C docstring behavior

2020-07-28 Thread James Corbett


Change by James Corbett :


--
keywords: +patch
pull_requests: +20816
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/21673

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



[issue41430] Document C docstring behavior

2020-07-28 Thread James Corbett


New submission from James Corbett :

As described in 
https://stackoverflow.com/questions/25847035/what-are-signature-and-text-signature-used-for-in-python-3-4,
 https://bugs.python.org/issue20586, and 
https://stackoverflow.com/questions/50537407/add-a-signature-with-annotations-to-extension-methods,
  it is possible to embed a signature in docstrings for C functions, so that 
`help` and `inspect.signature` work properly on them. However, this 
functionality isn't documented anywhere. I think something should be added to 
the "extending and embedding the Python interpreter" tutorial.

--
assignee: docs@python
components: Documentation
messages: 374547
nosy: docs@python, jameshcorbett
priority: normal
severity: normal
status: open
title: Document C docstring behavior
type: enhancement

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



[issue41046] unittest: make skipTest a classmethod

2020-07-28 Thread James Corbett


James Corbett  added the comment:

I was careless in my example, it would need to be `cls.skipTest(reason)`. 
However, that really doesn't have anything to do with why it should be a 
`classmethod` instead of an instance method: it's so that you can call 
`skipTest` from `classmethods`, namely `setUpClass` which is called by the 
`unittest` framework. You can currently call `skipTest` from `setUpClass` only 
with something ugly like `cls.skipTest(None, reason)` (i.e. passing `None` for 
the `self` parameter, which works because `self` isn't used).

--

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



[issue41047] argparse: misbehavior when combining positionals and choices

2020-06-19 Thread James Corbett


Change by James Corbett :


--
keywords: +patch
pull_requests: +20172
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/20997

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



[issue41047] argparse: misbehavior when combining positionals and choices

2020-06-19 Thread James Corbett


New submission from James Corbett :

The `argparse.ArgumentParser` sometimes rejects positional arguments with no 
arguments when `choices` is set and `nargs="*"`. 

When there are no arguments and `nargs` is `"*"`, the default value is chosen, 
or `[]` if there is no default value. This value is then checked against 
`choices` and an error message is printed if the value is not in `choices`. 
However, sometimes the value is intentionally not in `choices`, and this leads 
to problems. An example will explain this much better, and show that the issue 
only occurs with the particular combination of positionals, `nargs="*"`, and 
`choices`:

```
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("foo", choices=["a", "b", "c"], nargs="*")
>>> parser.add_argument("--bar", choices=["d", "e", "f"], nargs="*")
>>> parser.add_argument('--baz', type=int, choices=range(5, 10), default="20")
>>> parser.parse_args("a --bar".split())
Namespace(foo=['a'], bar=[], baz=20)
>>> parser.parse_args(["a"])
Namespace(foo=['a'], bar=None, baz=20)
>>> parser.parse_args([])
usage: [-h] [--bar [{d,e,f} ...]] [--baz {5,6,7,8,9}] [{a,b,c} ...]
: error: argument foo: invalid choice: [] (choose from 'a', 'b', 'c')
```

In this case I could have got around the last error by adding `[]` to choices, 
but that pollutes the help and usage messages.

--
components: Library (Lib)
messages: 371915
nosy: jameshcorbett
priority: normal
severity: normal
status: open
title: argparse: misbehavior when combining positionals and choices
type: behavior
versions: Python 3.9

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



[issue41046] unittest: make skipTest a classmethod

2020-06-19 Thread James Corbett


Change by James Corbett :


--
keywords: +patch
pull_requests: +20171
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/20996

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



[issue41046] unittest: make skipTest a classmethod

2020-06-19 Thread James Corbett


New submission from James Corbett :

The `unittest.TestCase.skipTest` method, used to skip the current test, is 
currently an instance method. There's nothing to stop it from being a 
`classmethod` or a `staticmethod` though---it doesn't use its reference to 
`self` since it's just a wrapper around the `SkipTest` exception. Making it a 
`classmethod` or `staticmethod` would allow calling the method from 
`setUpClass`. Here's an example:

```
import unittest

class MyTestCase(unittest.TestCase):

@classmethod
def ready_for_tests(cls):
pass

@classmethod
def setUpClass(cls):
if not cls.ready_for_tests():
cls.skipTest()
```

--
components: Library (Lib)
messages: 371914
nosy: jameshcorbett
priority: normal
severity: normal
status: open
title: unittest: make skipTest a classmethod
type: enhancement
versions: Python 3.9

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