From 590957a0eb9a87a6f2bd3463226b0a7f1405d817 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 7 Apr 2020 20:32:54 +0200 Subject: [PATCH] Fixed #31407 -- Adjusted test to avoid coroutine never awaited warning. --- tests/handlers/tests.py | 7 ++++--- tests/handlers/views.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index dac2d967f3..3d322ec6ed 100644 --- a/tests/handlers/tests.py +++ b/tests/handlers/tests.py @@ -269,9 +269,10 @@ class AsyncHandlerRequestTests(SimpleTestCase): async def test_unawaited_response(self): msg = ( - "The view handlers.views.async_unawaited didn't return an " - "HttpResponse object. It returned an unawaited coroutine instead. " - "You may need to add an 'await' into your view." + "The view handlers.views.CoroutineClearingView.__call__ didn't" + " return an HttpResponse object. It returned an unawaited" + " coroutine instead. You may need to add an 'await'" + " into your view." ) with self.assertRaisesMessage(ValueError, msg): await self.async_client.get('/unawaited/') diff --git a/tests/handlers/views.py b/tests/handlers/views.py index 9180c5e5a4..455532dd3c 100644 --- a/tests/handlers/views.py +++ b/tests/handlers/views.py @@ -51,6 +51,12 @@ async def async_regular(request): return HttpResponse(b'regular content') -async def async_unawaited(request): - """Return an unawaited coroutine (common error for async views).""" - return asyncio.sleep(0) +class CoroutineClearingView: + def __call__(self, request): + """Return an unawaited coroutine (common error for async views).""" + # Store coroutine to suppress 'unawaited' warning message + self._unawaited_coroutine = asyncio.sleep(0) + return self._unawaited_coroutine + + +async_unawaited = CoroutineClearingView()