コード例 #1
0
async def async_bind_awaitable(
    function: Callable[[_ValueType], Awaitable[_NewValueType]],
    inner_value: Awaitable[Result[_ValueType, _ErrorType]],
) -> Result[_NewValueType, _ErrorType]:
    """Async binds a coroutine over a value."""
    container = await inner_value
    if isinstance(container, Result.success_type):
        return Result.from_value(await function(container.unwrap()))
    return container  # type: ignore[return-value]
コード例 #2
0
ファイル: asserts.py プロジェクト: mysticfall/alleycat
def assert_failure(actual: Result[..., ...], expected: Any) -> None:
    if isinstance(expected, Exception):
        if isinstance(actual, Result.success_type):
            raise AssertionError(f"Expected a failure but got {actual}.")

        failure = actual.failure()

        if isinstance(failure, Exception):
            assert failure.__class__ == expected.__class__ and failure.args == expected.args, \
                f"Expected f{expected} but got {actual}."
        else:
            raise AssertionError(f"Expected f{expected} but got {actual}.")
    else:
        assert actual == Result.from_failure(expected)
コード例 #3
0
    def __call__(self, stream_id: UUID) -> Result[StreamDetails, FailureDetails]:
        stream = self.__find_stream_by_stream_id(stream_id).bind(
            self.__verify_if_stream_exist
        )
        partial_projects = partial(stream.bind, self.__find_projects_by_stream)
        partial_ksql = partial(
            stream.bind, lambda stream_: self.__get_stream_by_name(stream_.name)
        )

        # TODO: Removes every `type: ignore` after resolution of
        #  https://github.com/dry-python/returns/issues/410
        return flow(  # type: ignore
            Result.from_value(StreamDetails.build),  # type: ignore
            stream.apply,
            bind(self.__call_and_apply(partial_projects)),
            bind(self.__call_and_apply(partial_ksql)),
        )
コード例 #4
0
from returns.context import (
    Reader,
    ReaderFutureResult,
    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),
コード例 #5
0
ファイル: test_manager.py プロジェクト: mysticfall/alleycat
 def next_state(self, state: int) -> ResultE[int]:
     return Result.from_value(state + len(input_text))
コード例 #6
0
ファイル: test_manager.py プロジェクト: mysticfall/alleycat
 def init_state(self) -> ResultE[int]:
     return Result.from_value(0)
コード例 #7
0
ファイル: test_is_io.py プロジェクト: nsidnev/returns
def test_return_false_with_result_container():
    """Ensures `is_io` function will return False for Result."""
    assert is_io(Result.from_failure('failure')) is False
コード例 #8
0
ファイル: base.py プロジェクト: mysticfall/alleycat
    def start(self, args: OrderedDict) -> None:
        self._start_args = Result.from_value(args)

        self.logger.info("Created state: %s.", args)
コード例 #9
0
ファイル: base.py プロジェクト: mysticfall/alleycat
from abc import ABC
from collections import OrderedDict
from typing import Final, final

from bge.types import KX_GameObject, KX_PythonComponent
from returns.result import Result, ResultE

from alleycat.common import AlreadyDisposedError, LoggingSupport, NotStartedError
from alleycat.core import ArgumentsHolder

RESULT_NOT_STARTED: Final = Result.from_failure(
    NotStartedError("The proxy has not been started yet."))

RESULT_DISPOSED: Final = Result.from_failure(
    AlreadyDisposedError("The proxy instance has been disposed already."))


class BaseProxy(ArgumentsHolder, LoggingSupport, ABC):
    _start_args: ResultE[OrderedDict] = RESULT_NOT_STARTED

    @final
    @property
    def start_args(self) -> ResultE[OrderedDict]:
        return self._start_args

    @property
    def valid(self) -> bool:
        return isinstance(self.start_args, Result.success_type)

    def start(self, args: OrderedDict) -> None:
        self._start_args = Result.from_value(args)
コード例 #10
0
ファイル: test_mouse.py プロジェクト: mysticfall/alleycat
 def state_of(x, y, *args):
     return Result.from_value(MouseState(Point2D(x, y), set(args)))
コード例 #11
0
def test_collect_all_result(iterable, sequence):
    """Iterable for ``Result`` and ``CollectAll``."""
    assert Result.from_iterable(iterable, CollectAll) == sequence
コード例 #12
0
ファイル: test_fail_fast.py プロジェクト: timgates42/returns
def test_fail_fast_result(iterable, sequence):
    """Iterable for ``Result`` and ``FailFast``."""
    assert Result.from_iterable(iterable, FailFast) == sequence
コード例 #13
0
ファイル: asserts.py プロジェクト: mysticfall/alleycat
def assert_success(actual: Result[..., ...], expected: Any) -> None:
    assert actual == Result.from_value(expected)
コード例 #14
0
ファイル: mouse.py プロジェクト: mysticfall/alleycat
 def init_state(self) -> ResultE[MouseState]:
     return Result.from_value(MouseState(Point2D(0.5, 0.5), set()))
コード例 #15
0
ファイル: test_apply.py プロジェクト: t94j0/returns
def test_apply_with_result():
    """Ensures that functions can be composed and return type is correct."""
    applied = apply(Result.from_value(_function))

    assert applied(Success(1)) == Success('2')
    assert applied(Failure('s')) == Failure('s')
コード例 #16
0
def require_type(obj: Any, expected: Type[T]) -> ResultE[T]:
    if not isinstance(obj, not_empty(expected)):
        return Failure(create_error(obj, expected))

    return Result.from_value(obj)