diff --git a/django/contrib/redirects/middleware.py b/django/contrib/redirects/middleware.py index 26a49f3e32..e148c17693 100644 --- a/django/contrib/redirects/middleware.py +++ b/django/contrib/redirects/middleware.py @@ -12,7 +12,7 @@ class RedirectFallbackMiddleware(MiddlewareMixin): response_gone_class = HttpResponseGone response_redirect_class = HttpResponsePermanentRedirect - def __init__(self, get_response=None): + def __init__(self, get_response): if not apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured( "You cannot use RedirectFallbackMiddleware when " diff --git a/django/contrib/sessions/middleware.py b/django/contrib/sessions/middleware.py index 50627a663b..4de80a5858 100644 --- a/django/contrib/sessions/middleware.py +++ b/django/contrib/sessions/middleware.py @@ -10,9 +10,7 @@ from django.utils.http import http_date class SessionMiddleware(MiddlewareMixin): - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response): - def __init__(self, get_response=None): + def __init__(self, get_response): super().__init__(get_response) engine = import_module(settings.SESSION_ENGINE) self.SessionStore = engine.SessionStore diff --git a/django/middleware/cache.py b/django/middleware/cache.py index 97bb199eff..85de0e91c5 100644 --- a/django/middleware/cache.py +++ b/django/middleware/cache.py @@ -61,9 +61,7 @@ class UpdateCacheMiddleware(MiddlewareMixin): UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE so that it'll get called last during the response phase. """ - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response): - def __init__(self, get_response=None): + def __init__(self, get_response): super().__init__(get_response) self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS self.page_timeout = None @@ -124,9 +122,7 @@ class FetchFromCacheMiddleware(MiddlewareMixin): FetchFromCacheMiddleware must be the last piece of middleware in MIDDLEWARE so that it'll get called last during the request phase. """ - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response): - def __init__(self, get_response=None): + def __init__(self, get_response): super().__init__(get_response) self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS @@ -168,9 +164,7 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware): Also used as the hook point for the cache decorator, which is generated using the decorator-from-middleware utility. """ - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response, cache_timeout=None, page_timeout=None, **kwargs): - def __init__(self, get_response=None, cache_timeout=None, page_timeout=None, **kwargs): + def __init__(self, get_response, cache_timeout=None, page_timeout=None, **kwargs): super().__init__(get_response) # We need to differentiate between "provided, but using default value", # and "not provided". If the value is provided using a default, then diff --git a/django/middleware/security.py b/django/middleware/security.py index d923893dc5..f27c6804b9 100644 --- a/django/middleware/security.py +++ b/django/middleware/security.py @@ -6,9 +6,7 @@ from django.utils.deprecation import MiddlewareMixin class SecurityMiddleware(MiddlewareMixin): - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response): - def __init__(self, get_response=None): + def __init__(self, get_response): super().__init__(get_response) self.sts_seconds = settings.SECURE_HSTS_SECONDS self.sts_include_subdomains = settings.SECURE_HSTS_INCLUDE_SUBDOMAINS diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index b2c681b33c..bc715e91d3 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -89,10 +89,9 @@ class MiddlewareMixin: sync_capable = True async_capable = True - # RemovedInDjango40Warning: when the deprecation ends, replace with: - # def __init__(self, get_response): - def __init__(self, get_response=None): - self._get_response_none_deprecation(get_response) + def __init__(self, get_response): + if get_response is None: + raise ValueError('get_response must be provided.') self.get_response = get_response self._async_check() super().__init__() @@ -137,11 +136,3 @@ class MiddlewareMixin: thread_sensitive=True, )(request, response) return response - - def _get_response_none_deprecation(self, get_response): - if get_response is None: - warnings.warn( - 'Passing None for the middleware get_response argument is ' - 'deprecated.', - RemovedInDjango40Warning, stacklevel=3, - ) diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index 4d0392ae5c..ba03109b30 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -295,3 +295,7 @@ to remove usage of these features. * Support for the pre-Django 3.1 user sessions (that use the SHA-1 algorithm) is removed. + +* The ``get_request`` argument for + ``django.utils.deprecation.MiddlewareMixin.__init__()`` is required and + doesn't accept ``None``. diff --git a/tests/deprecation/test_middleware_mixin.py b/tests/deprecation/test_middleware_mixin.py index 4f410a77c8..ecaf4a5f6a 100644 --- a/tests/deprecation/test_middleware_mixin.py +++ b/tests/deprecation/test_middleware_mixin.py @@ -28,14 +28,10 @@ from django.middleware.http import ConditionalGetMiddleware from django.middleware.locale import LocaleMiddleware from django.middleware.security import SecurityMiddleware from django.test import SimpleTestCase -from django.utils.deprecation import MiddlewareMixin, RemovedInDjango40Warning +from django.utils.deprecation import MiddlewareMixin class MiddlewareMixinTests(SimpleTestCase): - """ - Deprecation warning is raised when using get_response=None. - """ - msg = 'Passing None for the middleware get_response argument is deprecated.' middlewares = [ AuthenticationMiddleware, BrokenLinkEmailsMiddleware, @@ -58,16 +54,11 @@ class MiddlewareMixinTests(SimpleTestCase): XViewMiddleware, ] - def test_deprecation(self): - for middleware in self.middlewares: - with self.subTest(middleware=middleware): - with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg): - middleware() - def test_passing_explicit_none(self): + msg = 'get_response must be provided.' for middleware in self.middlewares: with self.subTest(middleware=middleware): - with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg): + with self.assertRaisesMessage(ValueError, msg): middleware(None) def test_coroutine(self): diff --git a/tests/redirects_tests/tests.py b/tests/redirects_tests/tests.py index 7e683a0ab7..b9c5e04334 100644 --- a/tests/redirects_tests/tests.py +++ b/tests/redirects_tests/tests.py @@ -3,7 +3,9 @@ from django.contrib.redirects.middleware import RedirectFallbackMiddleware from django.contrib.redirects.models import Redirect from django.contrib.sites.models import Site from django.core.exceptions import ImproperlyConfigured -from django.http import HttpResponseForbidden, HttpResponseRedirect +from django.http import ( + HttpResponse, HttpResponseForbidden, HttpResponseRedirect, +) from django.test import TestCase, modify_settings, override_settings @@ -58,12 +60,15 @@ class RedirectTests(TestCase): @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}) def test_sites_not_installed(self): + def get_response(request): + return HttpResponse() + msg = ( 'You cannot use RedirectFallbackMiddleware when ' 'django.contrib.sites is not installed.' ) with self.assertRaisesMessage(ImproperlyConfigured, msg): - RedirectFallbackMiddleware() + RedirectFallbackMiddleware(get_response) class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):