Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Warn if schedule() callable returns an awaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Dec 31, 2019
1 parent f5048c9 commit 643e2ac
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion asyncio_resource/resource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import concurrent.futures
import inspect
import warnings
from typing import Awaitable, Callable, Generic, TypeVar

Expand Down Expand Up @@ -29,7 +30,15 @@ def schedule(self, fn: Callable[[R], T]) -> "concurrent.futures.Future[T]":
"""

async def invoke() -> T:
return fn(self._resource)
result = fn(self._resource)
if __debug__ and inspect.isawaitable(result):
warnings.warn(
RuntimeWarning(
f"Callable given to Resource.schedule returned an awaitable; did you mean schedule_async?"
)
)

return result

return asyncio.run_coroutine_threadsafe(invoke(), self._loop)

Expand Down

0 comments on commit 643e2ac

Please sign in to comment.