Skip to content

Commit 550b68e

Browse files
authored
Make anyio.run support Awaitable at runtime on all backends (#1171)
1 parent 29a5e04 commit 550b68e

5 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎docs/versionhistory.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
4040

4141
This reverts an earlier change from v3.7.0 which was made in error.
4242
(`#1153 <https://github.com/agronholm/anyio/pull/1153>`_)
43+
- Changed ``anyio.run`` to support callables returning arbitrary awaitables at runtime
44+
on all backends. Previously, this only worked on asyncio (`#1171
45+
<https://github.com/agronholm/anyio/pull/1171>`_; PR by @gschaffner)
4346
- Changed several classes (and their subclasses) to have ``__slots__`` (with
4447
``__weakref__``):
4548

‎src/anyio/_backends/_trio.py‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from contextlib import AbstractContextManager
2121
from contextvars import Context
2222
from dataclasses import dataclass
23-
from functools import partial
23+
from functools import partial, wraps
2424
from io import IOBase
2525
from os import PathLike
2626
from signal import Signals
@@ -104,6 +104,30 @@
104104
P = ParamSpec("P")
105105

106106

107+
def ensure_returns_coro(
108+
func: Callable[P, Awaitable[T_Retval]],
109+
) -> Callable[P, Coroutine[Any, Any, T_Retval]]:
110+
@wraps(func)
111+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, T_Retval]:
112+
awaitable = func(*args, **kwargs)
113+
# Check the common case first.
114+
if isinstance(awaitable, Coroutine):
115+
return awaitable
116+
elif not isinstance(awaitable, Awaitable):
117+
# The user violated the type annotations. Still, we should pass this on to
118+
# Trio so it can raise with an appropriate message.
119+
return awaitable
120+
else:
121+
122+
@wraps(func)
123+
async def inner_wrapper() -> T_Retval:
124+
return await awaitable
125+
126+
return inner_wrapper()
127+
128+
return wrapper
129+
130+
107131
#
108132
# Event loop
109133
#
@@ -930,7 +954,9 @@ def __init__(self, **options: Any) -> None:
930954
from queue import Queue
931955

932956
self._call_queue: Queue[Callable[[], object]] = Queue()
933-
self._send_stream: MemoryObjectSendStream | None = None
957+
self._send_stream: (
958+
MemoryObjectSendStream[tuple[Awaitable[Any], list[Outcome]]] | None
959+
) = None
934960
self._options = options
935961

936962
def __exit__(
@@ -948,11 +974,13 @@ def is_running(self) -> bool:
948974
return trio.lowlevel.in_trio_task()
949975

950976
async def _run_tests_and_fixtures(self) -> None:
951-
self._send_stream, receive_stream = create_memory_object_stream(1)
977+
self._send_stream, receive_stream = create_memory_object_stream[
978+
tuple[Awaitable[Any], list[Outcome]]
979+
](1)
952980
with receive_stream:
953-
async for coro, outcome_holder in receive_stream:
981+
async for awaitable, outcome_holder in receive_stream:
954982
try:
955-
retval = await coro
983+
retval = await awaitable
956984
except BaseException as exc:
957985
outcome_holder.append(Error(exc))
958986
else:
@@ -1044,7 +1072,7 @@ def run(
10441072
options: dict[str, Any],
10451073
) -> T_Retval:
10461074
assert not kwargs, "unreachable, and not supported by Trio"
1047-
return trio.run(func, *args, **options)
1075+
return trio.run(ensure_returns_coro(func), *args, **options)
10481076

10491077
@classmethod
10501078
def current_token(cls) -> object:

‎tests/conftest.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import platform
55
import ssl
66
import sys
7-
from collections.abc import Generator, Iterator
7+
from collections.abc import Awaitable, Callable, Coroutine, Generator, Iterator
88
from ssl import SSLContext
9-
from typing import TYPE_CHECKING, Any
9+
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar
1010
from unittest.mock import Mock
1111

1212
import pytest
@@ -20,6 +20,9 @@
2020
if TYPE_CHECKING:
2121
from blockbuster import BlockBuster
2222

23+
T = TypeVar("T")
24+
P = ParamSpec("P")
25+
2326
uvloop_marks = []
2427
uvloop_name = "uvloop"
2528
try:
@@ -178,3 +181,17 @@ async def event_loop_implementation_name() -> str | None:
178181
return asyncio.get_running_loop().__module__
179182

180183
return name
184+
185+
186+
def return_non_coro_awaitable(
187+
func: Callable[P, Coroutine[Any, Any, T]],
188+
) -> Callable[P, Awaitable[T]]:
189+
class Wrapper:
190+
def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
191+
self.args = args
192+
self.kwargs = kwargs
193+
194+
def __await__(self) -> Generator[Any, Any, T]:
195+
return func(*self.args, **self.kwargs).__await__()
196+
197+
return Wrapper

‎tests/test_eventloop.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
from asyncio import get_running_loop
77
from collections.abc import Generator
8+
from typing import Any
89
from unittest import mock
910
from unittest.mock import AsyncMock
1011

@@ -13,6 +14,8 @@
1314

1415
from anyio import current_time, run, sleep, sleep_forever, sleep_until
1516

17+
from .conftest import return_non_coro_awaitable
18+
1619
fake_current_time = 1620581544.0
1720

1821

@@ -52,6 +55,23 @@ async def async_add(x: int, y: int) -> int:
5255
assert result == 3
5356

5457

58+
def test_run_non_corofunc(
59+
anyio_backend_name: str, anyio_backend_options: dict[str, Any]
60+
) -> None:
61+
@return_non_coro_awaitable
62+
async def async_add(x: int, y: int) -> int:
63+
return x + y
64+
65+
result = run(
66+
async_add,
67+
1,
68+
2,
69+
backend=anyio_backend_name,
70+
backend_options=anyio_backend_options,
71+
)
72+
assert result == 3
73+
74+
5575
def test_run_unknown_backend() -> None:
5676
async def main() -> None:
5777
pass

‎tests/test_from_thread.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from anyio.from_thread import BlockingPortal, start_blocking_portal
3535
from anyio.lowlevel import EventLoopToken, checkpoint, current_token
3636

37-
from .conftest import asyncio_params
37+
from .conftest import asyncio_params, return_non_coro_awaitable
3838

3939
if sys.version_info < (3, 11):
4040
from exceptiongroup import ExceptionGroup
@@ -351,6 +351,13 @@ async def test_call_corofunc(self) -> None:
351351
result = await to_thread.run_sync(portal.call, async_add, 1, 2)
352352
assert result == 3
353353

354+
async def test_call_non_corofunc(self) -> None:
355+
async with BlockingPortal() as portal:
356+
result = await to_thread.run_sync(
357+
portal.call, return_non_coro_awaitable(async_add), 1, 2
358+
)
359+
assert result == 3
360+
354361
async def test_call_anext(self) -> None:
355362
gen = asyncgen_add(1, 2)
356363
try:
@@ -483,6 +490,17 @@ async def event_waiter() -> Literal["test"]:
483490
portal.call(event2.wait)
484491
assert future.result() == "test"
485492

493+
def test_start_task_soon_non_corofunc(
494+
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
495+
) -> None:
496+
@return_non_coro_awaitable
497+
async def taskfunc() -> Literal["test"]:
498+
return "test"
499+
500+
with start_blocking_portal(anyio_backend_name, anyio_backend_options) as portal:
501+
future = portal.start_task_soon(taskfunc)
502+
assert future.result() == "test"
503+
486504
def test_start_task_soon_cancel_later(
487505
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
488506
) -> None:
@@ -632,6 +650,18 @@ async def taskfunc(*, task_status: TaskStatus[str]) -> None:
632650
assert value == "foo"
633651
assert future.result() is None
634652

653+
def test_start_non_corofunc(
654+
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
655+
) -> None:
656+
@return_non_coro_awaitable
657+
async def taskfunc(*, task_status: TaskStatus[str]) -> None:
658+
task_status.started("foo")
659+
660+
with start_blocking_portal(anyio_backend_name, anyio_backend_options) as portal:
661+
future, value = portal.start_task(taskfunc)
662+
assert value == "foo"
663+
assert future.result() is None
664+
635665
def test_start_crash_before_started_call(
636666
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
637667
) -> None:

0 commit comments

Comments
 (0)