I need an efficient and pythonic way to check if a callable object is async or not
inspect.iscoroutinefunction fails to identify that, I've tried this:
import inspect
async def test_func() -> None:
pass
class TestClass:
async def __call__(self) -> None:
pass
test_obj = TestClass()
when testing:
inspect.iscoroutinefunction(test_func)
>>> True
inspect.iscoroutinefunction(test_obj)
>>> False
and when testing:
inspect.iscoroutinefunction(test_func.__call__)
>>> False
inspect.iscoroutinefunction(test_obj.__call__)
>>> True
I can make a helper function like:
def is_async(func: Callable) -> bool:
try:
return any(map(inspect.iscoroutinefunction, (func, func.__call__)))
except AttributeError:
return False
But I feel that there's something simpler...