From 7fb1f2ee559bb96b25db38ee01890dc17019c43d Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 14 Nov 2025 08:53:15 +0000 Subject: [PATCH] Fix asyncio.iscoroutinefunction() deprecation warnings Fix these warnings seen in the test run: ``` test middleware switches to async (__acall__) based on get_response type ... /home/runner/work/django-debug-toolbar/django-debug-toolbar/tests/test_middleware.py:57: DeprecationWarning: 'asyncio.iscoroutinefunction' is deprecated and slated for removal in Python 3.16; use inspect.iscoroutinefunction() instead self.assertTrue(asyncio.iscoroutinefunction(middleware)) test middleware switches to sync (__call__) based on get_response type ... /home/runner/work/django-debug-toolbar/django-debug-toolbar/tests/test_middleware.py:38: DeprecationWarning: 'asyncio.iscoroutinefunction' is deprecated and slated for removal in Python 3.16; use inspect.iscoroutinefunction() instead self.assertFalse(asyncio.iscoroutinefunction(middleware)) ``` [`inspect.iscoroutinefunction()`](https://docs.python.org/3/library/inspect.html#inspect.iscoroutinefunction) has always been the public API, since Python 3.5, though we need `markcoroutine()` support, which was added in Python 3.12. --- tests/test_middleware.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 56081ce56..72d4e3db6 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -1,4 +1,4 @@ -import asyncio +import sys from unittest.mock import patch from django.contrib.auth.models import User @@ -7,6 +7,11 @@ from debug_toolbar.middleware import DebugToolbarMiddleware +if sys.version_info >= (3, 12): + from inspect import iscoroutinefunction +else: + from asyncio import iscoroutinefunction + def show_toolbar_if_staff(request): # Hit the database, but always return True @@ -35,7 +40,7 @@ def test_sync_mode(self): lambda x: HttpResponse("Test app") ) - self.assertFalse(asyncio.iscoroutinefunction(middleware)) + self.assertFalse(iscoroutinefunction(middleware)) response = middleware(request) self.assertEqual(response.status_code, 200) @@ -54,7 +59,7 @@ async def get_response(request): middleware = DebugToolbarMiddleware(get_response) request = self.async_factory.get("/") - self.assertTrue(asyncio.iscoroutinefunction(middleware)) + self.assertTrue(iscoroutinefunction(middleware)) response = await middleware(request) self.assertEqual(response.status_code, 200)