Skip to content

Commit b0882b1

Browse files
committed
Added helper to detect async callables
1 parent 876b3cb commit b0882b1

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ellar/helper/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
import functools
13
import inspect
24
import re
35
import typing as t
@@ -29,3 +31,12 @@ def get_name(endpoint: t.Union[t.Callable, t.Type, object]) -> str:
2931
if inspect.isfunction(endpoint) or inspect.isclass(endpoint):
3032
return endpoint.__name__
3133
return endpoint.__class__.__name__
34+
35+
36+
def is_async_callable(obj: t.Any) -> bool:
37+
while isinstance(obj, functools.partial):
38+
obj = obj.func
39+
40+
return asyncio.iscoroutinefunction(obj) or (
41+
callable(obj) and asyncio.iscoroutinefunction(obj.__call__)
42+
)

0 commit comments

Comments
 (0)