Fix async resource functions not being awaited (#1092)

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
This commit is contained in:
Inna Harper 2025-07-06 10:22:52 +01:00 • committed by GitHub
commit 3d67e723e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,7 +54,12 @@ class FunctionResource(Resource):
async def read(self) -> str | bytes:
"""Read the resource by calling the wrapped function."""
try:
result = await self.fn() if inspect.iscoroutinefunction(self.fn) else self.fn()
# Call the function first to see if it returns a coroutine
result = self.fn()
# If it's a coroutine, await it
if inspect.iscoroutine(result):
result = await result
if isinstance(result, Resource):
return await result.read()
elif isinstance(result, bytes):