]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Use `asyncio.iscoroutinefunction` for Python 3.12 and older (#2984)
authorMartijn Pieters <github.com@zopatista.com>
Sun, 24 Aug 2025 07:01:30 +0000 (08:01 +0100)
committerGitHub <noreply@github.com>
Sun, 24 Aug 2025 07:01:30 +0000 (08:01 +0100)
starlette/_utils.py
tests/test__utils.py

index 5ac985de1dd1c49f0cdaf931de15da102162ebea..b0f01d31b755c07a38658d9c6ffc8a8ace3410fe 100644 (file)
@@ -1,7 +1,6 @@
 from __future__ import annotations
 
 import functools
-import inspect
 import sys
 from collections.abc import Awaitable, Generator
 from contextlib import AbstractAsyncContextManager, contextmanager
@@ -10,8 +9,11 @@ from typing import Any, Callable, Generic, Protocol, TypeVar, overload
 from starlette.types import Scope
 
 if sys.version_info >= (3, 13):  # pragma: no cover
+    from inspect import iscoroutinefunction
     from typing import TypeIs
 else:  # pragma: no cover
+    from asyncio import iscoroutinefunction
+
     from typing_extensions import TypeIs
 
 has_exceptiongroups = True
@@ -37,7 +39,7 @@ def is_async_callable(obj: Any) -> Any:
     while isinstance(obj, functools.partial):
         obj = obj.func
 
-    return inspect.iscoroutinefunction(obj) or (callable(obj) and inspect.iscoroutinefunction(obj.__call__))
+    return iscoroutinefunction(obj) or (callable(obj) and iscoroutinefunction(obj.__call__))
 
 
 T_co = TypeVar("T_co", covariant=True)
index 916f460d4112a8741f28cdda383a8bc0bcaddb34..42372ce3af6bd85f1042cb2d5f1ef35e037a4eba 100644 (file)
@@ -1,5 +1,6 @@
 import functools
 from typing import Any
+from unittest.mock import create_autospec
 
 import pytest
 
@@ -83,6 +84,13 @@ def test_async_nested_partial() -> None:
     assert is_async_callable(nested_partial)
 
 
+def test_async_mocked_async_function() -> None:
+    async def async_func() -> None: ...  # pragma: no cover
+
+    mock = create_autospec(async_func)
+    assert is_async_callable(mock)
+
+
 @pytest.mark.parametrize(
     "scope, expected_result",
     [