#36903 closed Bug (fixed)
inspect.signature should use FORWARDREF annotation_format for python 3.14+
| Reported by: | 93578237 | Owned by: | 93578237 |
|---|---|---|---|
| Component: | Core (Other) | Version: | 5.2 |
| Severity: | Release blocker | Keywords: | typing, inspect, deferred annotations |
| 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 (last modified by )
On python 3.14 it fails at least here:
https://github.com/django/django/blob/13299a6203f4bc3e5b2552c96a51ff2b15da3c43/django/contrib/auth/__init__.py#L43
import typing as t
from django.contrib.auth.backends import ModelBackend
if t.TYPE_CHECKING:
from django.http import HttpRequest
from testproject.models import User as UserModel
class AuthenticationBackend(ModelBackend):
def authenticate(
self,
request: HttpRequest | None = None,
username: str | None = None,
password: str | None = None,
**kwargs: t.Any,
) -> UserModel | None: ...
Maybe a new function signature should be added to utils/inspect.py
Change History (15)
comment:1 by , 4 weeks ago
| Description: | modified (diff) |
|---|
comment:2 by , 4 weeks ago
| Description: | modified (diff) |
|---|
comment:3 by , 4 weeks ago
| Description: | modified (diff) |
|---|
comment:4 by , 3 weeks ago
| Keywords: | typing inspect deferred annotations added |
|---|---|
| Severity: | Normal → Release blocker |
| Triage Stage: | Unreviewed → Accepted |
comment:5 by , 3 weeks ago
Would it be possible to backport it to 5.x? I encountered that on 5.2.x.
My proposal is to add the inspect function that passes an additional annotation_format kwarg if its PY_314
comment:6 by , 3 weeks ago
That decision will be made during review, but I'll note that prior related tickets were backported to 5.2.x.
comment:7 by , 3 weeks ago
I can work on it. Should it be fixed in-place or by reusing a separate function?
comment:8 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
A new function is appealing, since the last time we touched this, we ended up with bloat like this:
# As the annotations are not used in any case, inspect the signature with # FORWARDREF to leave any deferred annotations unevaluated. if PY314: signature = inspect.signature( func, annotation_format=annotationlib.Format.FORWARDREF ) else: signature = inspect.signature(func)
Now that we're aware of the multiple call sites that matter, DRY-ing this out makes sense to me.
Thanks for volunteering.
comment:9 by , 3 weeks ago
| Has patch: | set |
|---|
comment:10 by , 3 weeks ago
| Patch needs improvement: | set |
|---|---|
| Version: | 6.0 → 5.2 |
comment:11 by , 3 weeks ago
| Patch needs improvement: | unset |
|---|
comment:12 by , 3 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Thanks, looks like we missed a few in #36696 and #36712.
We need to audit uses of
inspect.signaturewith the possible exception of cases where we are only inspecting Django's own signatures e.g.deprecate_posargs(), but even that is debatable since there is motion toward making gradual explorations of incremental type annotations in Django.