Opened 2 years ago

Closed 2 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: master
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 Changed 2 years ago by Alexander Allakhverdiyev

Description: modified (diff)

comment:2 Changed 2 years ago by Alexander Allakhverdiyev

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

comment:3 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Alexander Allakhverdiyev

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 2 years ago by Alexander Allakhverdiyev (previous) (diff)

comment:6 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by Alexander Allakhverdiyev

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

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

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