Skip to content

Commit ec4e653

Browse files
authored
Fix asyncio.iscoroutinefunction() deprecation warnings (#2251)
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.
1 parent 5321f6d commit ec4e653

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

tests/test_middleware.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import asyncio
1+
import sys
22
from unittest.mock import patch
33

44
from django.contrib.auth.models import User
@@ -7,6 +7,11 @@
77

88
from debug_toolbar.middleware import DebugToolbarMiddleware
99

10+
if sys.version_info >= (3, 12):
11+
from inspect import iscoroutinefunction
12+
else:
13+
from asyncio import iscoroutinefunction
14+
1015

1116
def show_toolbar_if_staff(request):
1217
# Hit the database, but always return True
@@ -35,7 +40,7 @@ def test_sync_mode(self):
3540
lambda x: HttpResponse("<html><body>Test app</body></html>")
3641
)
3742

38-
self.assertFalse(asyncio.iscoroutinefunction(middleware))
43+
self.assertFalse(iscoroutinefunction(middleware))
3944

4045
response = middleware(request)
4146
self.assertEqual(response.status_code, 200)
@@ -54,7 +59,7 @@ async def get_response(request):
5459
middleware = DebugToolbarMiddleware(get_response)
5560
request = self.async_factory.get("/")
5661

57-
self.assertTrue(asyncio.iscoroutinefunction(middleware))
62+
self.assertTrue(iscoroutinefunction(middleware))
5863

5964
response = await middleware(request)
6065
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)