[Django] #33430: Calling command via `call_command` with option that is `required` and has set `dest` leads to an error

15 views
Skip to first unread message

Django

unread,
Jan 11, 2022, 8:24:08 AM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: micuda | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 3.2
(Management commands) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error:

"Error: the following arguments are required: <option>"

----

Code to simulate behaviour:

{{{
from django.core.management.base import BaseCommand, CommandParser

class Command(BaseCommand):
def add_arguments(self, parser: CommandParser):
# parser.add_argument('--from') # OK
# parser.add_argument('--from', dest='from_') # OK
# parser.add_argument('--from', required=True) # OK
parser.add_argument('--from', required=True, dest='from_') # leads
to an error

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

and calling the command:

{{{
call_command('bug_report', **{ 'from': '2022-01-11' })
}}}

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

Django

unread,
Jan 11, 2022, 10:00:51 AM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: Adam Mičuda | Owner: ravi
| kunwar
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* owner: nobody => ravi kunwar
* status: new => assigned


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

Django

unread,
Jan 11, 2022, 10:03:35 AM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: Adam Mičuda | Owner: ravi
| kunwar
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by ravi kunwar):

hey Adam Mičuda
whic python and django version you are using ??

--
Ticket URL: <https://code.djangoproject.com/ticket/33430#comment:2>

Django

unread,
Jan 11, 2022, 10:49:25 AM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: Adam Mičuda | Owner: ravi
| kunwar
Type: Bug | Status: closed

Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

You should pass `dest` in `options` as
[https://docs.djangoproject.com/en/dev/ref/django-admin/#running-
management-commands-from-your-code documented]:

> Some command options have different names when using `call_command()`
instead of `django-admin` or `manage.py`. For example, `django-admin
createsuperuser --no-input` translates to `call_command('createsuperuser',
interactive=False)`. To find what keyword argument name to use for
`call_command()`, check the command’s source code for the **`dest`**
argument passed to `parser.add_argument()`.

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

Django

unread,
Jan 11, 2022, 11:21:17 AM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: Adam Mičuda | Owner: ravi
| kunwar
Type: Bug | Status: closed
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Adam Mičuda):

Hi @Ravi,

I'm sorry for not providing these informations. I use Python 3.9.6 and
Django 3.2.10.


Hi @Mariusz,
Oh, you are right. Thank you. I have one note to this: I think the
behaviour is kind of confusing from dev experience.
Lets have an option with the `dest` set, e.g.
{{{


parser.add_argument('--from', dest='from_')
}}}

It is possible to call `call_command` as


{{{
call_command('bug_report', **{ 'from': '2022-01-11' })
}}}

and
{{{
call_command('bug_report', **{ 'from_': '2022-01-11' })
}}}
either. But if the option is `required` the first call is not possible. I
believe the behaviour should be consistent regardless of whether the
option is required or not. What do you think?

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

Django

unread,
Jan 11, 2022, 12:38:40 PM1/11/22
to django-...@googlegroups.com
#33430: Calling command via `call_command` with option that is `required` and has
set `dest` leads to an error
-------------------------------------+-------------------------------------
Reporter: Adam Mičuda | Owner: ravi
| kunwar
Type: Bug | Status: closed
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Mariusz Felisiak):

> But if the option is required the first call is not possible. I believe
the behaviour should be consistent regardless of whether the option is
required or not. What do you think?

The current behavior is consistent. In the first call, i.e.


{{{
call_command('bug_report', **{ 'from': '2022-01-11' })
}}}

`from` is simply ignored, so an error is raised only when it's required.

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

Reply all
Reply to author
Forward
0 new messages