fix: account for multiple times wrapped functions (#476)

This commit is contained in:
Joschka Braun
2024-03-01 18:04:45 -05:00
committed by GitHub
parent 3fc2cd1d61
commit 9aedf730d6
2 changed files with 39 additions and 3 deletions
+5 -3
View File
@@ -467,9 +467,11 @@ def retry_sync(
def is_async(func: Callable) -> bool:
"""Returns true if the callable is async, accounting for wrapped callables"""
return inspect.iscoroutinefunction(func) or (
hasattr(func, "__wrapped__") and inspect.iscoroutinefunction(func.__wrapped__)
)
is_coroutine = inspect.iscoroutinefunction(func)
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
is_coroutine = is_coroutine or inspect.iscoroutinefunction(func)
return is_coroutine
OVERRIDE_DOCS = """
+34
View File
@@ -39,6 +39,40 @@ def test_is_async_returns_true_if_wrapped_function_is_async():
assert is_async(wrapped_function) is True
def test_is_async_returns_true_if_double_wrapped_function_is_async():
async def async_function():
pass
@functools.wraps(async_function)
def wrapped_function():
pass
@functools.wraps(wrapped_function)
def double_wrapped_function():
pass
assert is_async(double_wrapped_function) is True
def test_is_async_returns_true_if_triple_wrapped_function_is_async():
async def async_function():
pass
@functools.wraps(async_function)
def wrapped_function():
pass
@functools.wraps(wrapped_function)
def double_wrapped_function():
pass
@functools.wraps(double_wrapped_function)
def triple_wrapped_function():
pass
assert is_async(triple_wrapped_function) is True
def test_override_docs():
assert (
"response_model" in OVERRIDE_DOCS