Opened 7 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#36712 closed Bug (fixed)

Deferred type annotations on template tags lead to TypeError with Python 3.14

Reported by: Patrick Rauscher Owned by: Jacob Walls
Component: Template system Version: 5.2
Severity: Release blocker Keywords: typing, inspect, deferred annotations
Cc: Patrick Rauscher 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

Similar to #36696 (which covered mainly Signals) this covers templatetags, where issues may happen when using SafeText:

from typing import TYPE_CHECKING

from django import template
from django.utils.html import escape

if TYPE_CHECKING:
    from django.utils.safestring import SafeText


register = template.Library()

@register.filter("example")
def example_filter(value: str) -> SafeText:
    return escape(value)

When using the templatetag, django/template/base.py line 763 (in django 5.2.8) will call inspect.getfullargspec for the filter, leading to a NameError which gets translated by inspect to a TypeError: unsupported callable. After searching a bit through the code this may also happen with views, as several parts of django use inspect directly for different checks (e.g. for templatetags it is used to check the number of arguments).

Not sure if this is a bug in python or django yet. One solution in django would be to replace direct calls to inspect.signature or inspect.getfullargspec (which uses inspect.signature internally according to python docs, but does not allow to specify an annotation_format.

Change History (14)

comment:1 by Augusto Pontes, 7 weeks ago

Owner: set to Augusto Pontes
Status: newassigned

comment:2 by Jacob Walls, 7 weeks ago

Component: UncategorizedTemplate system
Keywords: typing inspect deferred annotations added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for the report. We should migrate away from getfullargspec() anyway:

Note that signature() and Signature Object provide the recommended API for callable introspection, and support additional behaviours (like positional-only arguments) that are sometimes encountered in extension module APIs. This function is retained primarily for use in code that needs to maintain compatibility with the Python 2 inspect module API.

See also this deprecation proposal.

comment:3 by Augusto Pontes, 6 weeks ago

Hi patrick, after some looking into the problem, it seems that this more a python problem instead of a django problem, because i tested on these versions: 3.12.3, 3.13.2, and the TypeError didnt raised, only at the version 3.14 as you tested, im studying other alternatives to solve this specific problem, depending on my progress, i will open a PR to give a suggestion

comment:4 by Augusto Pontes, 6 weeks ago

Needs tests: set

comment:6 by Augusto Pontes, 6 weeks ago

Has patch: set

comment:7 by Simon Charette, 6 weeks ago

Summary: Deferred Annotations on Template Tags lead to TypeError with Python 3.14Deferred type annotations on template tags lead to TypeError with Python 3.14

Just renamed the ticket to avoid "deferred annotation" ambiguity with ORM language.

in reply to:  7 comment:8 by Augusto Pontes, 6 weeks ago

Replying to Simon Charette:

Just renamed the ticket to avoid "deferred annotation" ambiguity with ORM language.

Ok, thanks

comment:9 by Jacob Walls, 5 weeks ago

Has patch: unset
Needs tests: unset
Owner: changed from Augusto Pontes to Jacob Walls

comment:10 by Jacob Walls, 3 weeks ago

Has patch: set

comment:11 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin

Natalia approved on github and asked me to merge, marking RFC

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

Resolution: fixed
Status: assignedclosed

In 34186e73:

Fixed #36712 -- Evaluated type annotations lazily in template tag registration.

Ideally, this will be reverted when an upstream solution is available for
https://github.com/python/cpython/issues/141560.

Thanks Patrick Rauscher for the report and Augusto Pontes for the
first iteration and test.

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

In ec732745:

[6.0.x] Fixed #36712 -- Evaluated type annotations lazily in template tag registration.

Ideally, this will be reverted when an upstream solution is available for
https://github.com/python/cpython/issues/141560.

Thanks Patrick Rauscher for the report and Augusto Pontes for the
first iteration and test.

Backport of 34186e731ca20a2344b1f88fd543a854d6b13a00 from main.

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

In da1dfe64:

[5.2.x] Fixed #36712 -- Evaluated type annotations lazily in template tag registration.

Ideally, this will be reverted when an upstream solution is available for
https://github.com/python/cpython/issues/141560.

Thanks Patrick Rauscher for the report and Augusto Pontes for the
first iteration and test.

Backport of 34186e731ca20a2344b1f88fd543a854d6b13a00 from main.

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