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

**URL:** <https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553>\
**Category:** Django Internals\
**Created:** [August 22, 2025, 12:02pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553 "2025-08-22T12:02:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![theorangeone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theorangeone/32/21264_2.png) [@theorangeone](https://forum.djangoproject.com/u/theorangeone)\
**Post date:** [August 22, 2025, 12:02pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/1 "2025-08-22T12:02:16Z")

</div>

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

```python
@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.

Interested in what other people think of this before I open a ticket.

---

<div class="post-metadata">

**Author:** ![Lily-Foote](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@Lily-Foote](https://forum.djangoproject.com/u/Lily-Foote)\
**Post date:** [August 22, 2025, 12:05pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/2 "2025-08-22T12:05:31Z")

</div>

This makes sense to me. Especially if the check is cheap enough to avoid hurting startup time much, which I expect it would be.

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [August 22, 2025, 4:21pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/3 "2025-08-22T16:21:17Z")

</div>

There is already a method [`func_supports_parameter`](https://github.com/django/django/blob/b3166e1e15824aedb7a609dfda18ef36ea023d06/django/utils/inspect.py#L75-L76) in `django.utils.inspect` which seems to be what is required to implement this.

Also a similar validation is already performed by `Signal.connect` to ensure that the receiver function supports generic keyword arguments.

A benchmark is required to ensure that this does not slow-down existing applications with lots of custom simple-tags.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [September 27, 2025, 9:41pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/4 "2025-09-27T21:41:19Z")

</div>

Yes, this seems worth it. Did you start working on it @theorangeone ?

---

<div class="post-metadata">

**Author:** ![theorangeone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theorangeone/32/21264_2.png) [@theorangeone](https://forum.djangoproject.com/u/theorangeone)\
**Post date:** [September 30, 2025, 4:32pm UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/5 "2025-09-30T16:32:49Z")

</div>

I’ve had thoughts and done some poking, but nothing concrete. Given there’s clearly interest, I’ll get a Trac ticket together, and try refining it into a concrete patch.

---

<div class="post-metadata">

**Author:** ![theorangeone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theorangeone/32/21264_2.png) [@theorangeone](https://forum.djangoproject.com/u/theorangeone)\
**Post date:** [November 12, 2025, 9:21am UTC](https://forum.djangoproject.com/t/validate-template-tag-context-argument-at-definition-time-rather-than-compile-time/42553/6 "2025-11-12T09:21:06Z")

</div>

Trac ticket:

> **[\#36728 (Validate template tag context argument at definition time, rather...](https://code.djangoproject.com/ticket/36728)**
