Opened 19 hours ago

Closed 16 hours ago

Last modified 14 hours ago

#36978 closed Uncategorized (invalid)

Should django evaluate typing annotations at all?

Reported by: 93578237 Owned by:
Component: Core (Other) Version: 5.2
Severity: Normal Keywords: typing, inspect, deferred annotations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Jacob Walls, 16 hours ago

Component: UncategorizedCore (Other)
Resolution: invalid
Status: newclosed

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.

Last edited 16 hours ago by Jacob Walls (previous) (diff)

comment:2 by 93578237, 14 hours ago

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?

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