diff options
| author | 93578237 <43147888+93578237@users.noreply.github.com> | 2026-02-09 16:06:50 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-10 17:08:13 -0500 |
| commit | a4999ef1b9790a4c0e793cf0e5c464e9935c3c3a (patch) | |
| tree | f40aea6d8d811cb1ea3e8babc428debd2d175675 /tests/auth_tests | |
| parent | e9b85373580338c4878d3f930b52c361398065ad (diff) | |
[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.
Diffstat (limited to 'tests/auth_tests')
| -rw-r--r-- | tests/auth_tests/test_auth_backends.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index 32fb092cf4..45c3de27da 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, TypeAlias 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: + AnnotatedUsername: TypeAlias = str + AnnotatedPassword: TypeAlias = str + class FilteredExceptionReporter(ExceptionReporter): def get_traceback_frames(self): @@ -1279,6 +1286,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): """ |
