1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
"""
Represents the base interfaces for types that do fear-some async operations.
This type means that ``FutureResult`` can (and will!) fail with exceptions.
Use this type to mark that this specific async opetaion can fail.
"""
from __future__ import annotations
from abc import abstractmethod
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, TypeVar
from typing_extensions import Never, Self
from returns.interfaces.specific import future, ioresult
from returns.primitives.hkt import KindN
if TYPE_CHECKING:
from returns.future import Future, FutureResult # noqa: WPS433
_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')
_ThirdType = TypeVar('_ThirdType')
_UpdatedType = TypeVar('_UpdatedType')
_ValueType = TypeVar('_ValueType')
_ErrorType = TypeVar('_ErrorType')
_FutureResultLikeType = TypeVar(
'_FutureResultLikeType',
bound='FutureResultLikeN',
)
class FutureResultLikeN(
future.FutureLikeN[_FirstType, _SecondType, _ThirdType],
ioresult.IOResultLikeN[_FirstType, _SecondType, _ThirdType],
):
"""
Base type for ones that does look like ``FutureResult``.
But at the time this is not a real ``Future`` and cannot be awaited.
It is also cannot be unwrapped, because it is not a real ``IOResult``.
"""
__slots__ = ()
@abstractmethod
def bind_future_result(
self: _FutureResultLikeType,
function: Callable[
[_FirstType],
FutureResult[_UpdatedType, _SecondType],
],
) -> KindN[_FutureResultLikeType, _UpdatedType, _SecondType, _ThirdType]:
"""Allows to bind ``FutureResult`` functions over a container."""
@abstractmethod
def bind_async_future_result(
self: _FutureResultLikeType,
function: Callable[
[_FirstType],
Awaitable[FutureResult[_UpdatedType, _SecondType]],
],
) -> KindN[_FutureResultLikeType, _UpdatedType, _SecondType, _ThirdType]:
"""Allows to bind async ``FutureResult`` functions over container."""
@classmethod
@abstractmethod
def from_failed_future(
cls: type[_FutureResultLikeType],
inner_value: Future[_ErrorType],
) -> KindN[_FutureResultLikeType, _FirstType, _ErrorType, _ThirdType]:
"""Creates new container from a failed ``Future``."""
@classmethod
def from_future_result(
cls,
inner_value: FutureResult[_ValueType, _ErrorType],
) -> KindN[Self, _ValueType, _ErrorType, _ThirdType]:
"""Creates container from ``FutureResult`` instance."""
#: Type alias for kinds with two type arguments.
FutureResultLike2 = FutureResultLikeN[_FirstType, _SecondType, Never]
#: Type alias for kinds with three type arguments.
FutureResultLike3 = FutureResultLikeN[_FirstType, _SecondType, _ThirdType]
class FutureResultBasedN(
future.FutureBasedN[_FirstType, _SecondType, _ThirdType],
FutureResultLikeN[_FirstType, _SecondType, _ThirdType],
):
"""
Base type for real ``FutureResult`` objects.
They can be awaited.
Still cannot be unwrapped.
"""
__slots__ = ()
#: Type alias for kinds with two type arguments.
FutureResultBased2 = FutureResultBasedN[_FirstType, _SecondType, Never]
#: Type alias for kinds with three type arguments.
FutureResultBased3 = FutureResultBasedN[_FirstType, _SecondType, _ThirdType]
|