[issue43220] Explicit default required arguments with add_mutually_exclusive_group are rejected

2021-02-14 Thread Keith Smiley


Keith Smiley  added the comment:

Here's an example outside of argparse showing this is caused by the `is` 
comparison with interned string:

```
import sys

short_string = sys.argv[1]
short_default = '1'
long_string = sys.argv[2]
long_default = 'not-interned'

print(f"short comparisons: id1: {id(short_default)} id2: {id(short_string)}, 
eq: {short_default == short_string}, is: {short_default is short_string}")
print(f"long comparisons: id1: {id(long_default)} id2: {id(long_string)}, eq: 
{long_default == long_string}, is: {long_default is long_string}")
```

```
% ./python.exe /tmp/foo.py 1 not-interned
short comparisons: id1: 4523386416 id2: 4523386416, eq: True, is: True
long comparisons: id1: 4524440064 id2: 4523395296, eq: True, is: False
```

--

___
Python tracker 

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



[issue43220] Explicit default required arguments with add_mutually_exclusive_group are rejected

2021-02-13 Thread Keith Smiley


Change by Keith Smiley :


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

___
Python tracker 

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



[issue43220] Explicit default required arguments with add_mutually_exclusive_group are rejected

2021-02-13 Thread Keith Smiley


New submission from Keith Smiley :

With this code:

```
import argparse

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--foo", default="1")
group.add_argument("--bar")
args = parser.parse_args()
print(args)
```

When you explicitly pass `--foo 1`, it is treated as if no argument was passed:

```
% python3 /tmp/bug.py --foo 1
usage: bug.py [-h] (--foo FOO | --bar BAR)
bug.py: error: one of the arguments --foo --bar is required
```

I can't tell if this behavior is intentional, but it was surprising to me. It 
also seems to be somewhat based on the length of the default string. For 
example on my macOS machine if I change the default to `longerstring` it does 
not have this issue.

--
components: Library (Lib)
messages: 386934
nosy: keith
priority: normal
severity: normal
status: open
title: Explicit default required arguments with add_mutually_exclusive_group 
are rejected
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 

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