mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
fix: account for multiple times wrapped functions (#476)
This commit is contained in:
+5
-3
@@ -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 = """
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user