Opened 8 years ago

Closed 8 years ago

#28129 closed New feature (fixed)

Allow custom template tags to accept keyword only arguments

Reported by: Alexander Allakhverdiyev Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords: template, inspect
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alexander Allakhverdiyev)

Example tag with a keyword-only argument

@register.simple_tag(takes_context=True)
def my_tag(context, arg1, *, kwarg2='n/a'):
    return ''

Html code

<div>bla bla bla</div>
{% my_tag 'arg_1_value' kwarg_2='kwarg_value' %}

Result

django.template.exceptions.TemplateSyntaxError: 'my_tag' received unexpected keyword argument 'kwarg_2'

Change History (8)

comment:1 by Alexander Allakhverdiyev, 8 years ago

Description: modified (diff)

comment:2 by Alexander Allakhverdiyev, 8 years ago

Also we no longer need to use custom django.utils.inspect.getargspec anymore and can switch to python's inspect.getfullargspec()

comment:3 by Tim Graham, 8 years ago

Has patch: set
Summary: Custom template tag keyword only argumentsAllow custom template tags to accept keyword only arguments
Triage Stage: UnreviewedAccepted
Type: BugNew feature

comment:4 by Tim Graham, 8 years ago

Based on my understanding of the Python issue, I don't think we want to use ​inspect.getfullargspec() as this is a discouraged API that may be removed at some point.

comment:5 by Alexander Allakhverdiyev, 8 years ago

It was deprecated in python 3.5, but they had changed their mind in 3.6

Check out the description of this function specifically for 3.6 (and 3.7dev too)
https://docs.python.org/3.6/library/inspect.html#inspect.getfullargspec

Changed in version 3.6: This method was previously documented as deprecated in favour of signature() in Python 3.5, but that decision has been reversed in order to restore a clearly supported standard interface for single-source Python 2/3 code migrating away from the legacy getargspec() API.

Based on that discussion they might want to deprecate it at some point in the future. But personally, I don't think that's a good enough reason to have a complete re-implementation of the function.

Last edited 8 years ago by Alexander Allakhverdiyev (previous) (diff)

comment:6 by Tim Graham, 8 years ago

Okay, Nick confirmed that getfullargspec() won't be deprecated in the future. Could you submit a separate PR to replace django.utils.inspect.getargspec() with it? You can rebase the patch for this ticket after that's completed.

comment:7 by Alexander Allakhverdiyev, 8 years ago

Okay, thank you! I've submitted PR#8428 to use inspect.getfullargspec() and rebased the patch.

comment:8 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In a7c6c70:

Fixed #28129 -- Allowed custom template tags to use keyword-only arguments.

Note: See TracTickets for help on using tickets.
Back to Top