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
|
from typing import TypeVar, overload
from returns.functions import identity
from returns.interfaces.bindable import BindableN
from returns.maybe import Maybe, Nothing, Some
from returns.pipeline import is_successful
from returns.primitives.hkt import KindN, kinded
from returns.result import Failure, Result, Success
_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')
_ThirdType = TypeVar('_ThirdType')
_BindableKind = TypeVar('_BindableKind', bound=BindableN)
@kinded
def flatten(
container: KindN[
_BindableKind,
KindN[_BindableKind, _FirstType, _SecondType, _ThirdType],
_SecondType,
_ThirdType,
],
) -> KindN[_BindableKind, _FirstType, _SecondType, _ThirdType]:
"""
Joins two nested containers together.
Please, note that it will not join
two ``Failure`` for ``Result`` case
or two ``Nothing`` for ``Maybe`` case
(or basically any two error types) together.
.. code:: python
>>> from returns.converters import flatten
>>> from returns.io import IO
>>> from returns.result import Failure, Success
>>> assert flatten(IO(IO(1))) == IO(1)
>>> assert flatten(Success(Success(1))) == Success(1)
>>> assert flatten(Failure(Failure(1))) == Failure(Failure(1))
See also:
- https://bit.ly/2sIviUr
"""
return container.bind(identity)
def result_to_maybe(
result_container: Result[_FirstType, _SecondType],
) -> Maybe[_FirstType]:
"""
Converts ``Result`` container to ``Maybe`` container.
.. code:: python
>>> from returns.maybe import Some, Nothing
>>> from returns.result import Failure, Success
>>> assert result_to_maybe(Success(1)) == Some(1)
>>> assert result_to_maybe(Success(None)) == Some(None)
>>> assert result_to_maybe(Failure(1)) == Nothing
>>> assert result_to_maybe(Failure(None)) == Nothing
"""
if is_successful(result_container):
return Some(result_container.unwrap())
return Nothing
@overload
def maybe_to_result(
maybe_container: Maybe[_FirstType],
) -> Result[_FirstType, None]: ...
@overload
def maybe_to_result(
maybe_container: Maybe[_FirstType],
default_error: _SecondType,
) -> Result[_FirstType, _SecondType]: ...
def maybe_to_result(
maybe_container: Maybe[_FirstType],
default_error: _SecondType | None = None,
) -> Result[_FirstType, _SecondType | None]:
"""
Converts ``Maybe`` container to ``Result`` container.
With optional ``default_error`` to be used for ``Failure``'s error value.
.. code:: python
>>> from returns.maybe import Some, Nothing
>>> from returns.result import Failure, Success
>>> assert maybe_to_result(Some(1)) == Success(1)
>>> assert maybe_to_result(Some(None)) == Success(None)
>>> assert maybe_to_result(Nothing) == Failure(None)
>>> assert maybe_to_result(Nothing, 'error') == Failure('error')
"""
if is_successful(maybe_container):
return Success(maybe_container.unwrap())
return Failure(default_error)
|