コード例 #1
0
    def from_result(
        cls, inner_value: 'Result[_ValueType, _ErrorType]',
    ) -> 'RequiresContextIOResult[NoDeps, _ValueType, _ErrorType]':
        """
        Creates new container with ``Result`` as a unit value.

        .. code:: python

          >>> from returns.context import RequiresContextIOResult
          >>> from returns.result import Success, Failure
          >>> from returns.io import IOSuccess, IOFailure
          >>> deps = RequiresContextIOResult.empty

          >>> assert RequiresContextIOResult.from_result(
          ...    Success(1),
          ... )(deps) == IOSuccess(1)

          >>> assert RequiresContextIOResult.from_result(
          ...    Failure(1),
          ... )(deps) == IOFailure(1)

        """
        return RequiresContextIOResult(
            lambda _: IOResult.from_result(inner_value),
        )
コード例 #2
0
    def from_io(
        cls,
        inner_value: IO[_NewValueType],
    ) -> RequiresContextIOResult[_NewValueType, Any, NoDeps]:
        """
        Creates new container from successful ``IO`` value.

        .. code:: python

          >>> from returns.io import IO, IOSuccess
          >>> from returns.context import RequiresContextIOResult

          >>> assert RequiresContextIOResult.from_io(IO(1))(
          ...     RequiresContextIOResult.empty,
          ... ) == IOSuccess(1)

        """
        return RequiresContextIOResult(
            lambda deps: IOResult.from_io(inner_value), )
コード例 #3
0
    def from_failed_io(
        cls,
        inner_value: IO[_NewErrorType],
    ) -> RequiresContextIOResult[Any, _NewErrorType, NoDeps]:
        """
        Creates a new container from failed ``IO`` value.

        .. code:: python

          >>> from returns.io import IO, IOFailure
          >>> from returns.context import RequiresContextIOResult

          >>> assert RequiresContextIOResult.from_failed_io(IO(1))(
          ...     RequiresContextIOResult.empty,
          ... ) == IOFailure(1)

        """
        return RequiresContextIOResult(
            lambda deps: IOResult.from_failed_io(inner_value), )
コード例 #4
0
ファイル: future.py プロジェクト: thepabloaguilar/returns
    async def awaitable(self) -> IOResult[_ValueType, _ErrorType]:
        """
        Transforms ``FutureResult[a, b]`` to ``Awaitable[IOResult[a, b]]``.

        Use this method when you need a real coroutine.
        Like for ``asyncio.run`` calls.

        Note, that returned value will be wrapped
        in :class:`returns.io.IOResult` container.

        .. code:: python

          >>> import anyio
          >>> from returns.future import FutureResult
          >>> from returns.io import IOSuccess
          >>> assert anyio.run(
          ...     FutureResult.from_value(1).awaitable,
          ... ) == IOSuccess(1)

        """
        return IOResult.from_result(await self._inner_value)
コード例 #5
0
    def from_result_context(
        cls,
        inner_value: 'RequiresContextResult[_ValueType, _ErrorType, _EnvType]',
    ) -> 'RequiresContextIOResult[_ValueType, _ErrorType, _EnvType]':
        """
        Creates new container from ``RequiresContextResult`` as a unit value.

        .. code:: python

          >>> from returns.context import RequiresContextResult
          >>> from returns.io import IOSuccess, IOFailure

          >>> assert RequiresContextIOResult.from_result_context(
          ...     RequiresContextResult.from_value(1),
          ... )(...) == IOSuccess(1)

          >>> assert RequiresContextIOResult.from_result_context(
          ...     RequiresContextResult.from_failure(1),
          ... )(...) == IOFailure(1)

        """
        return RequiresContextIOResult(
            lambda deps: IOResult.from_result(inner_value(deps)), )
コード例 #6
0
    ReaderIOResult,
    ReaderResult,
)
from returns.contrib.pytest import ReturnsAsserts
from returns.future import Future, FutureResult
from returns.io import IO, IOResult
from returns.maybe import Maybe
from returns.primitives.asserts import assert_equal
from returns.primitives.container import BaseContainer
from returns.result import Result

_containers: Sequence[BaseContainer] = (
    Result.from_failure(1),
    Result.from_value(1),
    IO(1),
    IOResult.from_failure(1),
    IOResult.from_value(1),
    Maybe.from_value(1),
    Maybe.from_value(None),
    Maybe.from_optional(None),
    Future.from_value(1),
    FutureResult.from_value(1),
    FutureResult.from_failure(1),
    Reader.from_value(1),
    ReaderResult.from_value(1),
    ReaderResult.from_failure(1),
    ReaderIOResult.from_value(1),
    ReaderIOResult.from_failure(1),
    ReaderFutureResult.from_value(1),
    ReaderFutureResult.from_failure(1),
)
コード例 #7
0
ファイル: test_is_io.py プロジェクト: nsidnev/returns
def test_return_true_with_io_result_container():
    """Ensures `is_io` function will return True for IOResult."""
    assert is_io(IOResult(Success(10))) is True
コード例 #8
0
ファイル: test_io.py プロジェクト: zeta1999/returns
def test_io_typecast_reverse(container):
    """Ensures that IO can be casted to IOResult and back."""
    assert IO.from_ioresult(container) == IO.from_ioresult(
        IOResult.from_typecast(IO.from_ioresult(container)),
    )
コード例 #9
0
ファイル: managed.py プロジェクト: zeta1999/returns
 def factory(deps):
     return _managed(
         lambda inner: self._use(inner)(deps),
         lambda inner, pure: self._release(inner, pure)(deps),
     )(IOResult.from_value(inner_value))
コード例 #10
0
def test_collect_all_ioresult(iterable, sequence):
    """Iterable for ``IOResult`` and ``CollectAll``."""
    assert IOResult.from_iterable(iterable, CollectAll) == sequence
コード例 #11
0
ファイル: test_fail_fast.py プロジェクト: timgates42/returns
def test_fail_fast_ioresult(iterable, sequence):
    """Iterable for ``IOResult`` and ``FailFast``."""
    assert IOResult.from_iterable(iterable, FailFast) == sequence
コード例 #12
0
ファイル: test_apply.py プロジェクト: t94j0/returns
def test_apply_with_ioresult():
    """Ensures that functions can be composed and return type is correct."""
    applied = apply(IOResult.from_value(_function))

    assert applied(IOSuccess(1)) == IOSuccess('2')
    assert applied(IOFailure('a')) == IOFailure('a')