Esempio n. 1
0
 def __call__(self, user_id: int) -> IO[Result[float, Exception]]:
     """Fetches `UserProfile` TypedDict from foreign API."""
     return flow(
         user_id,
         self._make_request,
         IO.lift(bind(self._parse_json)),
     )
Esempio n. 2
0
from returns.maybe import Nothing, Some
from returns.result import Failure, Success


def _success_case(state: int) -> int:
    return state + 1


def _failed_case(_) -> int:
    return 0


_result_converter = coalesce_result(_success_case, _failed_case)
_maybe_converter = coalesce_maybe(_success_case, _failed_case)
_ioresult_converter = coalesce_ioresult(
    IO.lift(_success_case),
    IO.lift(_failed_case),
)


def test_coalesce_result():
    """Ensures that `coalesce` is always returning the correct type."""
    assert _result_converter(Success(1)) == 2
    assert _result_converter(Failure(1)) == 0


def test_coalesce_ioresult():
    """Ensures that `coalesce` is always returning the correct type."""
    assert _ioresult_converter(IOSuccess(1)) == IO(2)
    assert _ioresult_converter(IOFailure(1)) == IO(0)
Esempio n. 3
0
def test_lift_io():
    """Ensures that functions can be composed and return type is correct."""
    lifted = IO.lift(_first)

    assert lifted(IO(1)) == IO('2')