|
| 1 | +import asyncio |
1 | 2 | from abc import abstractmethod |
2 | 3 | from collections.abc import Awaitable, Callable, Generator |
3 | 4 | from dataclasses import dataclass |
4 | | -from typing import Any, NoReturn, Protocol, runtime_checkable |
| 5 | +from typing import Any, Protocol, runtime_checkable |
5 | 6 |
|
6 | 7 |
|
7 | 8 | @dataclass(repr=False, eq=False, frozen=True, slots=True) |
@@ -32,10 +33,16 @@ class AsyncCaller[**P, T](Caller[P, T]): |
32 | 33 | async def acall(self, /, *args: P.args, **kwargs: P.kwargs) -> T: |
33 | 34 | return await self.callable(*args, **kwargs) |
34 | 35 |
|
35 | | - def call(self, /, *args: P.args, **kwargs: P.kwargs) -> NoReturn: |
36 | | - raise RuntimeError( |
37 | | - "Synchronous call isn't supported for an asynchronous Callable." |
38 | | - ) |
| 36 | + def call(self, /, *args: P.args, **kwargs: P.kwargs) -> T: |
| 37 | + loop = asyncio.get_event_loop() |
| 38 | + |
| 39 | + if loop.is_running(): |
| 40 | + raise RuntimeError( |
| 41 | + "Can't call an asynchronous function in a synchronous context." |
| 42 | + ) |
| 43 | + |
| 44 | + coroutine = self.callable(*args, **kwargs) |
| 45 | + return loop.run_until_complete(coroutine) |
39 | 46 |
|
40 | 47 |
|
41 | 48 | @dataclass(repr=False, eq=False, frozen=True, slots=True) |
|
0 commit comments