Refs #26601 -- Made get_response argument required and don't accept None in middleware classes.

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2021-01-12 20:55:32 +01:00
parent 6b4941dd57
commit 4bb30fe5d5
8 changed files with 23 additions and 42 deletions

View File

@ -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 "

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,
)

View File

@ -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``.

View File

@ -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):

View File

@ -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):