Skip to content

Commit

Permalink
fix: account for multiple times wrapped functions (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschkabraun authored Mar 1, 2024
1 parent 3fc2cd1 commit 9aedf73
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 5 additions & 3 deletions instructor/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
34 changes: 34 additions & 0 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ def wrapped_function():
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
Expand Down

0 comments on commit 9aedf73

Please sign in to comment.