You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected #87386
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.
changed the title [-]Explicit default required arguments with add_mutually_exclusive_group are rejected[/-][+]Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected[/+]on Feb 15, 2021
That issue shows that this problem arises with small integers as well (<257), which in cpython have unique ids. It's an implementation detail, pypy for example does not have this issue.
The whole purpose of this extra default testing is to allow '?/*' positionals in mutually_exclusive_groups.
The patch I proposed in 2013 is basically the same thing, except I called the new flag variable 'using_default'.
We should review the discussion in that issue to see if it raises any additional issues or concerns.
Activity
keith commentedon Feb 14, 2021
With this code:
When you explicitly pass
--foo 1, it is treated as if no argument was passed: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
longerstringit does not have this issue.keith commentedon Feb 14, 2021
Here's an example outside of argparse showing this is caused by the
iscomparison with interned string:[-]Explicit default required arguments with add_mutually_exclusive_group are rejected[/-][+]Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected[/+]4 remaining items
paulj3 commentedon Apr 7, 2021
An overlapping issue
https://bugs.python.org/issue18943
argparse: default args in mutually exclusive groups
That issue shows that this problem arises with small integers as well (<257), which in cpython have unique ids. It's an implementation detail, pypy for example does not have this issue.
The whole purpose of this extra default testing is to allow '?/*' positionals in mutually_exclusive_groups.
The patch I proposed in 2013 is basically the same thing, except I called the new flag variable 'using_default'.
We should review the discussion in that issue to see if it raises any additional issues or concerns.
serhiy-storchaka commentedon Sep 25, 2024
Closed as a duplicate of #63143.