Skip to content

Commit 3d7430a

Browse files
authored
Use asyncio.iscoroutinefunction for Python 3.12 and older (#2984)
1 parent 6ee94f2 commit 3d7430a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

starlette/_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import functools
4-
import inspect
54
import sys
65
from collections.abc import Awaitable, Generator
76
from contextlib import AbstractAsyncContextManager, contextmanager
@@ -10,8 +9,11 @@
109
from starlette.types import Scope
1110

1211
if sys.version_info >= (3, 13): # pragma: no cover
12+
from inspect import iscoroutinefunction
1313
from typing import TypeIs
1414
else: # pragma: no cover
15+
from asyncio import iscoroutinefunction
16+
1517
from typing_extensions import TypeIs
1618

1719
has_exceptiongroups = True
@@ -37,7 +39,7 @@ def is_async_callable(obj: Any) -> Any:
3739
while isinstance(obj, functools.partial):
3840
obj = obj.func
3941

40-
return inspect.iscoroutinefunction(obj) or (callable(obj) and inspect.iscoroutinefunction(obj.__call__))
42+
return iscoroutinefunction(obj) or (callable(obj) and iscoroutinefunction(obj.__call__))
4143

4244

4345
T_co = TypeVar("T_co", covariant=True)

tests/test__utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
from typing import Any
3+
from unittest.mock import create_autospec
34

45
import pytest
56

@@ -83,6 +84,13 @@ async def async_func(
8384
assert is_async_callable(nested_partial)
8485

8586

87+
def test_async_mocked_async_function() -> None:
88+
async def async_func() -> None: ... # pragma: no cover
89+
90+
mock = create_autospec(async_func)
91+
assert is_async_callable(mock)
92+
93+
8694
@pytest.mark.parametrize(
8795
"scope, expected_result",
8896
[

0 commit comments

Comments
 (0)