Opened 4 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#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 93578237)

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 93578237, 4 weeks ago

Description: modified (diff)

comment:2 by 93578237, 4 weeks ago

Description: modified (diff)

comment:3 by 93578237, 4 weeks ago

Description: modified (diff)

comment:4 by Jacob Walls, 3 weeks ago

Keywords: typing inspect deferred annotations added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Thanks, looks like we missed a few in #36696 and #36712.

We need to audit uses of inspect.signature with 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.

comment:5 by 93578237, 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 Jacob Walls, 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 93578237, 3 weeks ago

I can work on it. Should it be fixed in-place or by reusing a separate function?

comment:8 by Jacob Walls, 3 weeks ago

Owner: set to 93578237
Status: newassigned

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 93578237, 3 weeks ago

Has patch: set

comment:10 by Jacob Walls, 3 weeks ago

Patch needs improvement: set
Version: 6.05.2

comment:11 by 93578237, 3 weeks ago

Patch needs improvement: unset

comment:12 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: assignedclosed

In 56ed37e1:

Fixed #36903 -- Fixed further NameErrors when inspecting functions with deferred annotations.

Provide a wrapper for safe introspection of user functions on Python 3.14+.

Follow-up to 601914722956cc41f1f2c53972d669ddee6ffc04.

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

In deac58e:

[6.0.x] Fixed #36903 -- Fixed further NameErrors when inspecting functions with deferred annotations.

Provide a wrapper for safe introspection of user functions on Python 3.14+.

Follow-up to 601914722956cc41f1f2c53972d669ddee6ffc04.
Backport of 56ed37e17e5b1a509aa68a0c797dcff34fcc1366 from main.

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

In a4999ef:

[5.2.x] Fixed #36903 -- Fixed further NameErrors when inspecting functions with deferred annotations.

Provide a wrapper for safe introspection of user functions on Python 3.14+.

Follow-up to 601914722956cc41f1f2c53972d669ddee6ffc04.
Backport of 56ed37e17e5b1a509aa68a0c797dcff34fcc1366 from main.

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