diff --git a/packages/toolbox-core/src/toolbox_core/utils.py b/packages/toolbox-core/src/toolbox_core/utils.py index 92660c84b..f92b9290b 100644 --- a/packages/toolbox-core/src/toolbox_core/utils.py +++ b/packages/toolbox-core/src/toolbox_core/utils.py @@ -14,6 +14,7 @@ import asyncio +import inspect import warnings from typing import ( Any, @@ -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 diff --git a/packages/toolbox-core/tests/test_utils.py b/packages/toolbox-core/tests/test_utils.py index ec6ebb22b..127247fd2 100644 --- a/packages/toolbox-core/tests/test_utils.py +++ b/packages/toolbox-core/tests/test_utils.py @@ -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"