[Django] #36978: Should django evaluate typing annotations at all?

16 views
Skip to first unread message

Django

unread,
Mar 8, 2026, 8:29:28 AMMar 8
to django-...@googlegroups.com
#36978: Should django evaluate typing annotations at all?
-------------------------------------+-------------------------------------
Reporter: 93578237 | Type:
| Uncategorized
Status: new | Component:
| Uncategorized
Version: 5.2 | Severity: Normal
Keywords: typing, inspect, | Triage Stage:
deferred annotations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I previously opened a bug report #36903 related to deferred annotations in
Python 3.14. I believe Django does not need to evaluate annotations using
annotationlib.Format.FORWARDREF and could instead use
annotationlib.Format.STRING.
--
Ticket URL: <https://code.djangoproject.com/ticket/36978>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 8, 2026, 11:37:30 AMMar 8
to django-...@googlegroups.com
#36978: Should django evaluate typing annotations at all?
-------------------------------------+-------------------------------------
Reporter: 93578237 | Owner: (none)
Type: Uncategorized | Status: closed
Component: Core (Other) | Version: 5.2
Severity: Normal | Resolution: invalid
Keywords: typing, inspect, | Triage Stage:
deferred annotations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* component: Uncategorized => Core (Other)
* resolution: => invalid
* status: new => closed

Comment:

Thanks, but I don't quite follow, doesn't `FORWARDREF` leave the
annotations unevaluated? I think I benched this at the time and found it
was faster than `STRING. It's also more friendly for any package extending
Django's behavior, since it makes fewer assumptions.
--
Ticket URL: <https://code.djangoproject.com/ticket/36978#comment:1>

Django

unread,
Mar 8, 2026, 12:57:05 PMMar 8
to django-...@googlegroups.com
#36978: Should django evaluate typing annotations at all?
-------------------------------------+-------------------------------------
Reporter: 93578237 | Owner: (none)
Type: Uncategorized | Status: closed
Component: Core (Other) | Version: 5.2
Severity: Normal | Resolution: invalid
Keywords: typing, inspect, | Triage Stage:
deferred annotations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by 93578237):

{{{
from inspect import *; from annotationlib import *; from typing import *

if TYPE_CHECKING: x = int

def foo(a: x) -> None: ...

In [4]: %timeit signature(foo, annotation_format=Format.FORWARDREF)
25 μs ± 445 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [5]: %timeit signature(foo, annotation_format=Format.STRING)
12.4 μs ± 495 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
}}}

What am I missing?
--
Ticket URL: <https://code.djangoproject.com/ticket/36978#comment:2>

Django

unread,
Mar 9, 2026, 11:06:55 AMMar 9
to django-...@googlegroups.com
#36978: Should django evaluate typing annotations at all?
-------------------------------------+-------------------------------------
Reporter: 93578237 | Owner: (none)
Type: Uncategorized | Status: closed
Component: Core (Other) | Version: 5.2
Severity: Normal | Resolution: invalid
Keywords: typing, inspect, | Triage Stage:
deferred annotations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

Good question. Sorry, I wasn't trying to hide details, I just needed to
recall what I benched. I benched the case where the annotations are
available (e.g. not guarded under `TYPE_CHECKING`, which is IMO more
likely):

{{{#!py
In [13]: class Imported:
...: ...
...:

In [14]: def foo(a: Imported, b: Imported, c: Imported): ...

In [15]: %timeit signature(foo, annotation_format=Format.FORWARDREF)
4.52 μs ± 177 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [16]: %timeit signature(foo, annotation_format=Format.STRING)
7.59 μs ± 37.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops
each)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36978#comment:3>
Reply all
Reply to author
Forward
0 new messages