From dc86a25a677d05703e0bb021b178e44412cea7e9 Mon Sep 17 00:00:00 2001 From: Jonny Park Date: Wed, 10 Mar 2021 14:33:52 +0900 Subject: [PATCH] Refs #24121 -- Added __repr__() to MiddlewareMixin and subclasses. --- django/utils/deprecation.py | 10 ++++++++ tests/deprecation/test_middleware_mixin.py | 27 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index 05913ab4ba..08f33fa7d3 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -96,6 +96,16 @@ class MiddlewareMixin: self._async_check() super().__init__() + def __repr__(self): + return '<%s get_response=%s>' % ( + self.__class__.__qualname__, + getattr( + self.get_response, + '__qualname__', + self.get_response.__class__.__name__, + ), + ) + def _async_check(self): """ If get_response is a coroutine function, turns us into async mode so diff --git a/tests/deprecation/test_middleware_mixin.py b/tests/deprecation/test_middleware_mixin.py index ecaf4a5f6a..cf9fbda35c 100644 --- a/tests/deprecation/test_middleware_mixin.py +++ b/tests/deprecation/test_middleware_mixin.py @@ -54,6 +54,33 @@ class MiddlewareMixinTests(SimpleTestCase): XViewMiddleware, ] + def test_repr(self): + class GetResponse: + def __call__(self): + return HttpResponse() + + def get_response(): + return HttpResponse() + + self.assertEqual( + repr(MiddlewareMixin(GetResponse())), + '', + ) + self.assertEqual( + repr(MiddlewareMixin(get_response)), + '.get_response>', + ) + self.assertEqual( + repr(CsrfViewMiddleware(GetResponse())), + '', + ) + self.assertEqual( + repr(CsrfViewMiddleware(get_response)), + '.get_response>', + ) + def test_passing_explicit_none(self): msg = 'get_response must be provided.' for middleware in self.middlewares: