[Django] #30584: call_command doesn't recognize subparsers for parameter validation

240 views
Skip to first unread message

Django

unread,
Jun 21, 2019, 9:44:05 AM6/21/19
to django-...@googlegroups.com
#30584: call_command doesn't recognize subparsers for parameter validation
-------------------------------------+-------------------------------------
Reporter: bparquet | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 2.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 |
-------------------------------------+-------------------------------------
If a management command contains subparsers:

{{{#!python
class Command(BaseCommand):
def add_arguments(self, parser):
subparsers = parser.add_subparsers(title="subcommands",
dest="subcommand",
required=True)
foo = subparsers.add_parser("foo")
foo.set_defaults(method=self.on_foo_command)
foo.add_argument("--bar")
}}}

In Django, 1.11, this could be called using

{{{#!python
call_command('mycommand', 'foo', bar=True)
}}}

With the additional argument validation in call_command, this generates
`ValueError: min() arg is an empty sequence` at line 124 in
`django/core/management/__init__.py` because the
`_SubParsersAction.option_strings` is an empty array.

{{{#!python
parse_args += [
'{}={}'.format(min(opt.option_strings), arg_options[opt.dest])
for opt in parser._actions if opt.required and opt.dest in options
]
}}}

If the subcommand parser is not tagged as required, `TypeError: Unknown
option(s) for mycommand command: bar` occurs downstream.

The same occurs if the subcommand is passed as an option:
{{{#!python
call_command('mycommand', subcommand='foo', bar=True)
}}}

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

Django

unread,
Jun 24, 2019, 8:35:42 AM6/24/19
to django-...@googlegroups.com
#30584: call_command raises ValueError when subparser dest is passed in options.
-------------------------------------+-------------------------------------
Reporter: bill parquet | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
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 (added)
* stage: Unreviewed => Accepted
* version: 2.2 => master


Comment:

Thanks for the report.

In Django 1.11 your custom command raises `TypeError: __init__() missing 1
required positional argument: 'cmd'` which has been fixed in
dd68b51e1da54267bde4799fa0d9fbd4290eb8b5. I wasn't able to run your
example in Django < 2.2.

`call_command('mycommand', 'foo', bar=True)` raises `TypeError: Unknown
option(s) for mycommand command: bar.` which seems fine for me because you
added only `--bar` argument, i.e.
{{{
>>> management.call_command('mycommand', 'foo', '--bar', '1')
>>> management.call_command('mycommand', 'foo', '--bar', True)
}}}
work as expected.

IMO there is only issue with validation of `call_command('mycommand',
subcommand='foo', bar=True)` because it raises
{{{
File "django/core/management/__init__.py", line 130, in <listcomp>


for opt in parser._actions if opt.required and opt.dest in options

ValueError: min() arg is an empty sequence
}}}

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

Django

unread,
Jun 26, 2019, 5:09:49 PM6/26/19
to django-...@googlegroups.com
#30584: call_command raises ValueError when subparser dest is passed in options.
-------------------------------------+-------------------------------------
Reporter: bill parquet | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
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


Comment:

I think `call_command('mycommand', 'foo', bar=True)` is a valid call. and
as we can see in the question the problem is because the


`_SubParsersAction.option_strings` is an empty array.

so we can fix it by adding a check for `option_strings`.

If we change the question command `foo` argument to `foo.add_argument("--
bar", required=True)` and call it again with `call_command('mycommand',
'foo', bar=True)` , we will face with problem because in the below section
of code

{{{


parse_args += [
'{}={}'.format(min(opt.option_strings), arg_options[opt.dest])
for opt in parser._actions if opt.required and opt.dest in options
]
}}}

we just loop over the `parser._actions`. but `bar` is not in the parser
actions. it is in the choices of foo:
`parser._actions[action_number].choices['foo']._actions`

Also, I think the `call_command('mycommand', subcommand='foo', bar=True)`
is invalid. because it is also invalid in python shell:

{{{
>>> import argparse
>>> parser = argparse.ArgumentParser()


>>> subparsers = parser.add_subparsers(title="subcommands",
dest="subcommand", required=True)
>>> foo = subparsers.add_parser("foo")

>>> foo.add_argument("--bar")
_StoreAction(option_strings=['--bar'], dest='bar', nargs=None, const=None,
default=None, type=None, choices=None, help=None, metavar=None)
>>> print(parser.parse_args(['subcommand=foo', '--bar=True']))
usage: [-h] {foo} ...
: error: argument subcommand: invalid choice: 'subcommand=foo' (choose
from 'foo')

}}}

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

Django

unread,
Jun 28, 2019, 7:13:09 AM6/28/19
to django-...@googlegroups.com
#30584: call_command raises ValueError when subparser dest is passed in options.
-------------------------------------+-------------------------------------
Reporter: bill parquet | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
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:"2b03e8e9e8205ae3a3aa128764277e70b7c30803" 2b03e8e9]:
{{{
#!CommitTicketReference repository=""
revision="2b03e8e9e8205ae3a3aa128764277e70b7c30803"
Fixed #30584 -- Fixed management command when using subparsers with dest
parameter.
}}}

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

Reply all
Reply to author
Forward
0 new messages