Opened 6 weeks ago

Closed 3 weeks ago

#36728 closed Cleanup/optimization (fixed)

Validate template tag context argument at definition time, rather than compile time

Reported by: Jake Howard Owned by: Jake Howard
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a template tag is defined incorrectly, by not accepting a context argument with takes_context=True:

@register.simple_tag(takes_context=True)
def my_tag():
    pass

An error is only raised when the template is compiled, rather than when the tag is defined as someone may expect. This means there’s extra work done at compile time, which is duplicated each time the tag is used, across multiple templates.

Moving this validation to be done at definition time would likely improve performance. Reading the function signature is already done at definition time, so the performance improvement will likely only be checking the list of arguments, but reporting the error earlier would probably also be a DX improvement.

See also forum thread.

Change History (6)

comment:1 by Natalia Bidart, 6 weeks ago

Triage Stage: UnreviewedAccepted

Thank you Jake! Once the fix is in place, I would like to see some before/after benchmarks for the startup time to confirm the assumption that this won't hurt it.

comment:2 by Jake Howard, 6 weeks ago

Has patch: set

PR

As mentioned in the PR description, because the majority of the work was already being done eagerly, the performance benefit is likely to be within margin of error for Python benchmarks (it's just a list member check). Therefore, I've not done any benchmarks currently.

comment:3 by Jacob Walls, 3 weeks ago

Patch needs improvement: set

comment:4 by Jake Howard, 3 weeks ago

Patch needs improvement: unset

comment:5 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:6 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 0ac54863:

Fixed #36728 -- Validated template tag arguments at definition time.

Before, context and content were validated at compile time.

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