Container to easily compose ``async`` functions. Represents a better abstraction over a simple coroutine. Is framework, event-loop, and IO-library agnostics. Works with ``asyncio``, ``curio``, ``trio``, or any other tool. Internally we use ``anyio`` to test that it works a
| 66 | |
| 67 | @final |
| 68 | class Future( # type: ignore[type-var] |
| 69 | BaseContainer, |
| 70 | SupportsKind1['Future', _ValueType_co], |
| 71 | FutureBased1[_ValueType_co], |
| 72 | ): |
| 73 | """ |
| 74 | Container to easily compose ``async`` functions. |
| 75 | |
| 76 | Represents a better abstraction over a simple coroutine. |
| 77 | |
| 78 | Is framework, event-loop, and IO-library agnostics. |
| 79 | Works with ``asyncio``, ``curio``, ``trio``, or any other tool. |
| 80 | Internally we use ``anyio`` to test |
| 81 | that it works as expected for any io stack. |
| 82 | |
| 83 | Note that ``Future[a]`` represents a computation |
| 84 | that never fails and returns ``IO[a]`` type. |
| 85 | Use ``FutureResult[a, b]`` for operations that might fail. |
| 86 | Like DB access or network operations. |
| 87 | |
| 88 | Is not related to ``asyncio.Future`` in any kind. |
| 89 | |
| 90 | .. rubric:: Tradeoffs |
| 91 | |
| 92 | Due to possible performance issues we move all coroutines definitions |
| 93 | to a separate module. |
| 94 | |
| 95 | See also: |
| 96 | - https://gcanti.github.io/fp-ts/modules/Task.ts.html |
| 97 | - https://zio.dev/docs/overview/overview_basic_concurrency |
| 98 | |
| 99 | """ |
| 100 | |
| 101 | __slots__ = () |
| 102 | |
| 103 | _inner_value: Awaitable[_ValueType_co] |
| 104 | |
| 105 | def __init__(self, inner_value: Awaitable[_ValueType_co]) -> None: |
| 106 | """ |
| 107 | Public constructor for this type. Also required for typing. |
| 108 | |
| 109 | .. code:: python |
| 110 | |
| 111 | >>> import anyio |
| 112 | >>> from returns.future import Future |
| 113 | >>> from returns.io import IO |
| 114 | |
| 115 | >>> async def coro(arg: int) -> int: |
| 116 | ... return arg + 1 |
| 117 | |
| 118 | >>> container = Future(coro(1)) |
| 119 | >>> assert anyio.run(container.awaitable) == IO(2) |
| 120 | |
| 121 | """ |
| 122 | super().__init__(ReAwaitable(inner_value)) |
| 123 | |
| 124 | def __await__(self) -> Generator[None, None, IO[_ValueType_co]]: |
| 125 | """ |
no outgoing calls
no test coverage detected