Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/toolbox-core/src/toolbox_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


import asyncio
import inspect
import warnings
from typing import (
Any,
Expand Down Expand Up @@ -165,7 +166,10 @@ async def resolve_value(
if asyncio.iscoroutinefunction(source):
return await source()
elif callable(source):
return source()
res = source()
if inspect.isawaitable(res):
return await res
return res
return source


Expand Down
17 changes: 17 additions & 0 deletions packages/toolbox-core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,23 @@ async def another_async_func():
assert await resolve_value(another_async_func) == {"key": "value"}


@pytest.mark.asyncio
async def test_resolve_value_callable_returning_awaitable():
"""Test resolving callables that return an awaitable/coroutine, such as callable objects or lambdas."""

class CallableAsyncGetter:
async def __call__(self) -> str:
return "callable_instance_result"

async def async_func() -> str:
return "lambda_result"

lambda_getter = lambda: async_func()

assert await resolve_value(CallableAsyncGetter()) == "callable_instance_result"
assert await resolve_value(lambda_getter) == "lambda_result"


def test_warn_if_http_and_headers_triggers():
"""Test that a warning is emitted for HTTP URLs with headers."""
url = "http://example.com"
Expand Down
Loading