[Django] #32047: Management commands: Mutually exclusive argument groups don't work with 'store_true' arguments & call_command

65 views
Skip to first unread message

Django

unread,
Sep 25, 2020, 8:53:18 PM9/25/20
to django-...@googlegroups.com
#32047: Management commands: Mutually exclusive argument groups don't work with
'store_true' arguments & call_command
-------------------------------------+-------------------------------------
Reporter: Mark | Owner: nobody
Gajdosik |
Type: Bug | Status: new
Component: Core | Version: 3.1
(Management commands) | Keywords: call_command,
Severity: Normal | exclusive group, store_true,
Triage Stage: | action, explicit, error
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have the following management command (called `test.py`):

{{{
from django.core.management import BaseCommand


class Command(BaseCommand):
def add_arguments(self, parser):
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--value', type=str)
group.add_argument('--flag', action='store_true')

def handle(self, *args, **options):
pass
}}}

Running `./manage.py test --flag` or `./manage.py --value foo`, the
command works as expected. Using `call_command` to call the command from
my code fails if I pass `--flag`, e.g.:

{{{
from django.core.management import call_command

call_command('test', flag=True)
}}}

This gets me the following traceback:

{{{
argparse.ArgumentError: argument --flag: ignored explicit argument 'True'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/__init__.py", line 147, in
call_command
defaults = parser.parse_args(args=parse_args)
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/base.py", line 57, in parse_args
return super().parse_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 1780, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.8/argparse.py", line 1819, in parse_known_args
self.error(str(err))
File "/home/mark/.local/share/virtualenvs/backend-KlVhwr95/lib/python3.8
/site-packages/django/core/management/base.py", line 63, in error
raise CommandError("Error: %s" % message)
django.core.management.base.CommandError: Error: argument --flag: ignored
explicit argument 'True'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32047>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 25, 2020, 8:59:46 PM9/25/20
to django-...@googlegroups.com
#32047: Management commands: Mutually exclusive argument groups don't work with
'store_true' arguments & call_command
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage:
exclusive group, store_true, | Unreviewed
action, explicit, error |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mark Gajdosik):

Probably stating the obvious, but I get the same error when running the
command from console and passing value to the `--flag` explicitly, e.g.
`./manage.py test --flag=True`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:1>

Django

unread,
Sep 28, 2020, 12:22:29 AM9/28/20
to django-...@googlegroups.com
#32047: Mutually exclusive argument groups don't work with 'store_true' arguments &
call_command
-------------------------------------+-------------------------------------

Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: closed

Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution: invalid

Keywords: call_command, | Triage Stage:
exclusive group, store_true, | Unreviewed
action, explicit, error |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by felixxm):

Mark's response via email:

> 1. I'm not using your trac as a support channel. This was the first time
I used it and I doubt I'll use it again.
> 2. RE the actual issue: docs for call_comamnd clearly state: "command
options that take no arguments are passed as keywords with True or False".
This works fine, until the arg is placed inside a group. The docs don't
mention this.
>
> I understand that Django devs are busy, but at least try understanding
what the ticket is saying before rushing to close it. Thank you.
>
> Kind Regards,
> Mark Gajdosik

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:3>

Django

unread,
Sep 28, 2020, 12:50:02 AM9/28/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work boolean arguments.

-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution: invalid
Keywords: call_command, | Triage Stage: Accepted
exclusive group, store_true, |
action, explicit, error |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* cc: Hasan Ramezani, Alex Tomic (added)
* stage: Unreviewed => Accepted


Comment:

> 1. I'm not using your trac as a support channel. This was the first time
I used it and I doubt I'll use it again.

I'm sorry you had a bad experience. From the ticket description I assumed
that you're trying to find a way to pass a boolean argument, that's why I
took it as a support question.

> 2. RE the actual issue: docs for call_comamnd clearly state: "command
options that take no arguments are passed as keywords with True or False".
This works fine, until the arg is placed inside a group. The docs don't
mention this.

Good catch, I can reproduce this issue for a required mutually exclusive
groups with boolean arguments. We should probably change
[https://github.com/django/django/blob/91669cc566918fde4fe98f867cf4c406f64504e6/django/core/management/__init__.py#L140-L146
preparing boolean arguments] before passing them to `parse_args()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:4>

Django

unread,
Sep 28, 2020, 12:53:14 AM9/28/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work boolean arguments.
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: new

Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, store_true, |
action, explicit, error |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: closed => new
* resolution: invalid =>


--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:5>

Django

unread,
Sep 28, 2020, 12:53:45 AM9/28/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work with boolean arguments.

-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: nobody
Type: Bug | Status: new

Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, store_true, |
action, explicit, error |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:6>

Django

unread,
Sep 29, 2020, 11:22:09 AM9/29/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work with boolean arguments.
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, store_true, |
action, explicit, error |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:8>

Django

unread,
Sep 30, 2020, 5:11:26 AM9/30/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work with boolean arguments.
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution:
Keywords: call_command, | Triage Stage: Accepted
exclusive group, store_true, |
action, explicit, error |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:9>

Django

unread,
Sep 30, 2020, 2:34:58 PM9/30/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work with boolean arguments.
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution: fixed
Keywords: call_command, | Triage Stage: Ready for
exclusive group, store_true, | checkin
action, explicit, error |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"6eb3f53bdd5089e3ab229780fe339fecfc30a7ee" 6eb3f53b]:
{{{
#!CommitTicketReference repository=""
revision="6eb3f53bdd5089e3ab229780fe339fecfc30a7ee"
Fixed #32047 -- Fixed call_command() crash if a constant option from
required mutually exclusive group is passed in options.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:11>

Django

unread,
Oct 6, 2020, 2:56:21 AM10/6/20
to django-...@googlegroups.com
#32047: Required mutually exclusive groups don't work with boolean arguments.
-------------------------------------+-------------------------------------
Reporter: Mark Gajdosik | Owner: Hasan
| Ramezani
Type: Bug | Status: closed
Component: Core (Management | Version: 3.1
commands) |
Severity: Normal | Resolution: fixed
Keywords: call_command, | Triage Stage: Ready for
exclusive group, store_true, | checkin
action, explicit, error |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"efe74fff25e1e85402ecb73d80eea7625246f4ea" efe74ff]:
{{{
#!CommitTicketReference repository=""
revision="efe74fff25e1e85402ecb73d80eea7625246f4ea"
Refs #32047 -- Added test for using call_command() with constant required
options.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32047#comment:12>

Reply all
Reply to author
Forward
0 new messages