From deac58ed822c6cd819e916e6c3d1736c7adfa31e Mon Sep 17 00:00:00 2001 From: 93578237 <43147888+93578237@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:06:50 -0500 Subject: [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. --- tests/auth_tests/test_auth_backends.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tests/auth_tests/test_auth_backends.py') diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index 0ba169249b..3ea6ff6a69 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -1,5 +1,7 @@ import sys +import unittest from datetime import date +from typing import TYPE_CHECKING from unittest import mock from unittest.mock import patch @@ -30,6 +32,7 @@ from django.test import ( override_settings, ) from django.urls import reverse +from django.utils.version import PY314 from django.views.debug import ExceptionReporter, technical_500_response from django.views.decorators.debug import sensitive_variables @@ -41,6 +44,10 @@ from .models import ( UUIDUser, ) +if TYPE_CHECKING: + type AnnotatedUsername = str + type AnnotatedPassword = str + class FilteredExceptionReporter(ExceptionReporter): def get_traceback_frames(self): @@ -1285,6 +1292,29 @@ class AuthenticateTests(TestCase): def test_skips_backends_with_decorated_method(self): self.assertEqual(authenticate(username="test", password="test"), self.user1) + @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") + @override_settings( + AUTHENTICATION_BACKENDS=[ + "auth_tests.test_auth_backends.AnnotatedBackend", + ], + ) + def test_backend_uses_deferred_annotations(self): + class AnnotatedBackend: + invariant_user = self.user1 + + def authenticate( + self, + request: HttpRequest, + username: AnnotatedUsername, + password: AnnotatedPassword, + ) -> User | None: + return self.invariant_user + + with unittest.mock.patch( + "django.contrib.auth.import_string", return_value=AnnotatedBackend + ): + self.assertEqual(authenticate(username="test", password="test"), self.user1) + class ImproperlyConfiguredUserModelTest(TestCase): """ -- cgit v1.3