def test_bind_with_ioresult(): """Ensures that functions can be composed and return type is correct.""" bound = bind(_ioresult_function) assert bound(IOSuccess(1)) == IOSuccess('2') assert bound(IOFailure('a')) == IOFailure('a') assert bound(IOSuccess(1)) == unify(_ioresult_function)(IOSuccess(1))
def test_rescue_with_ioresult(): """Ensures that functions can be composed and return type is correct.""" rescued = rescue(_ioresult_function) assert rescued(IOSuccess(1)) == IOSuccess(1) assert rescued(IOFailure(1)) == IOSuccess(2) assert rescued(IOFailure(0)) == IOFailure('nope')
def test_not_equals(): """Ensures that ``.equals`` method works correctly.""" inner_value = 1 assert not IOSuccess(inner_value).equals(IOFailure(inner_value)) assert not IOSuccess(inner_value).equals(IOSuccess(0)) assert not IOFailure(inner_value).equals(IOSuccess(inner_value)) assert not IOFailure(inner_value).equals(IOFailure(0))
def test_rescue_success(): """Ensures that rescue works for IOSuccess container.""" def factory(inner_value) -> IOResult[int, str]: return IOSuccess(inner_value * 2) bound = IOSuccess(5).rescue(factory) assert bound == IOSuccess(5)
def test_rescue_with_context_ioresult(): """Ensures that functions can be composed and return type is correct.""" rescued = rescue(_context_ioresult_function) assert rescued(RequiresContextIOResult.from_value(1), )(1) == IOSuccess(1) assert rescued( RequiresContextIOResult.from_failure(1), )(1) == IOSuccess(2) assert rescued( RequiresContextIOResult.from_failure(0), )(1) == IOFailure('nope')
def test_bind_regular_context(): """Ensures that regular ``RequiresContext`` can be bound.""" def factory(inner_value: int) -> RequiresContext[float, int]: return RequiresContext(lambda deps: inner_value / deps) first: RCR[int, str, int] = RCR.from_value(1) third: RCR[int, str, int] = RCR.from_failure('a') assert first.bind_context(factory)(2) == IOSuccess(0.5) assert RCR.from_value(2).bind_context(factory, )(1) == IOSuccess(2.0) assert third.bind_context(factory)(1) == IOFailure('a')
def test_bind_result_context(): """Ensures that ``RequiresContextResult`` can be bound.""" def factory(inner_value: int) -> RequiresContextResult[int, float, str]: return RequiresContextResult(lambda deps: Success(inner_value / deps)) first: RCR[int, int, str] = RCR.from_success(1) third: RCR[int, int, str] = RCR.from_failure('a') assert first.bind_context_result(factory)(2) == IOSuccess(0.5) assert RCR.from_success(2).bind_context_result( factory, )(1) == IOSuccess(2.0) assert third.bind_context_result(factory)(1) == IOFailure('a')
def test_bind_regular_result(): """Ensures that regular ``Result`` can be bound to ``IOResult``.""" def factory(inner_value: int) -> Result[int, str]: if inner_value > 0: return Success(inner_value + 1) return Failure('nope') first: IOResult[int, str] = IOSuccess(1) second: IOResult[int, str] = IOSuccess(0) third: IOResult[int, str] = IOFailure('a') assert first.bind_result(factory) == IOSuccess(2) assert second.bind_result(factory) == IOFailure('nope') assert third.bind_result(factory) == IOFailure('a')
def __call__( self, inner_value: str, use_result: Result[str, str], ) -> IOResult[None, str]: self._logs.append((inner_value, use_result)) return IOSuccess(None)
def test_ioresulte(): """Ensures that IOResultE correctly typecast.""" container: IOResult[float, Exception] = _function(1) assert container == IOSuccess(10.0) container = _function(0) assert is_successful(container) is False
def test_unify_and_bind(): """Ensures that left identity works for IOSuccess container.""" def factory(inner_value: int) -> IOResult[int, str]: return IOSuccess(inner_value * 2) bound: IOResult[int, str] = IOSuccess(5) assert bound.unify(factory) == bound.bind(factory) assert bound.bind(factory) == bound.unify(factory)
def test_left_identity_success(): """Ensures that left identity works for IOSuccess container.""" def factory(inner_value: int) -> IOResult[int, str]: return IOSuccess(inner_value * 2) input_value = 5 bound: IOResult[int, str] = IOSuccess(input_value) assert bound.bind(factory) == factory(input_value)
def test_bind(): """Ensures that bind works.""" def factory(inner_value: int) -> IOResult[int, str]: if inner_value > 0: return IOSuccess(inner_value * 2) return IOFailure(str(inner_value)) input_value = 5 bound: IOResult[int, str] = IOSuccess(input_value) assert bound.bind(factory) == factory(input_value) assert str(bound.bind(factory)) == '<IOResult: <Success: 10>>' input_value = 0 bound2: IOResult[int, str] = IOSuccess(input_value) assert bound2.bind(factory) == factory(input_value) assert str(bound2.bind(factory)) == '<IOResult: <Failure: 0>>'
def test_full_typing(): """This test is here to be a case for typing.""" logs: List[Tuple[str, Result[str, str]]] = [] pipeline_result = managed( _use_success, _ReleaseSuccess(logs), )(_acquire_success) assert pipeline_result(ReaderIOResult.no_args) == IOSuccess('use success') assert logs == [('acquire success', Success('use success'))]
async def test_full_typing(): """This test is here to be a case for typing.""" logs: List[Tuple[str, Result[str, str]]] = [] pipeline_result = managed( _use_success, _ReleaseSuccess(logs), )(_acquire_success()) inner = pipeline_result(ReaderFutureResult.empty) assert await inner == IOSuccess('use success') assert logs == [('acquire success', Success('use success'))]
def test_bind(): """Ensures that bind works.""" def factory(inner_value: int) -> RCR[float, str, int]: if inner_value > 0: return RCR(lambda deps: IOSuccess(inner_value / deps)) return RCR.from_failure(str(inner_value)) input_value = 5 bound: RCR[int, str, int] = RCR.from_value(input_value) assert bound.bind(factory)(2) == factory(input_value)(2) assert bound.bind(factory)(2) == IOSuccess(2.5) assert RCR.from_value(0).bind( factory, )(2) == factory(0)(2) == IOFailure('0')
def test_bind_ioresult(): """Ensures that io ``Result`` can be bound.""" def factory(inner_value: int) -> IOResult[int, str]: if inner_value > 0: return IOSuccess(inner_value + 1) return IOFailure('nope') first: RCR[int, str, int] = RCR.from_value(1) third: RCR[int, str, int] = RCR.from_failure('a') assert first.bind_ioresult(factory)(RCR.empty) == IOSuccess(2) assert RCR.from_value(0).bind_ioresult(factory, )( RCR.empty) == IOFailure('nope') assert third.bind_ioresult(factory)(RCR.empty) == IOFailure('a')
def from_value( cls, inner_value: _FirstType, ) -> 'RequiresContextIOResult[NoDeps, _FirstType, Any]': """ Creates new container with ``IOSuccess(inner_value)`` as a unit value. .. code:: python >>> from returns.context import RequiresContextIOResult >>> from returns.io import IOSuccess >>> assert RequiresContextIOResult.from_value(1)( ... RequiresContextIOResult.empty, ... ) == IOSuccess(1) """ return RequiresContextIOResult(lambda _: IOSuccess(inner_value))
def from_value( cls, inner_value: _NewValueType, ) -> RequiresContextIOResult[_NewValueType, Any, NoDeps]: """ Creates new container with ``IOSuccess(inner_value)`` as a unit value. .. code:: python >>> from returns.context import RequiresContextIOResult >>> from returns.io import IOSuccess >>> assert RequiresContextIOResult.from_value(1)( ... RequiresContextIOResult.no_args, ... ) == IOSuccess(1) """ return RequiresContextIOResult(lambda _: IOSuccess(inner_value))
def from_context( cls, inner_value: 'RequiresContext[_EnvType, _FirstType]', ) -> 'RequiresContextIOResult[_EnvType, _FirstType, Any]': """ Creates new container from ``RequiresContext`` as a success unit. .. code:: python >>> from returns.context import RequiresContext >>> from returns.io import IOSuccess >>> assert RequiresContextIOResult.from_context( ... RequiresContext.from_value(1), ... )(...) == IOSuccess(1) """ return RequiresContextIOResult( lambda deps: IOSuccess(inner_value(deps)), )
async def test_inner_value(subtests): """Ensure that coroutine correct value is preserved for all units.""" containers = [ # We have to define these values inside the test, because # otherwise `anyio` will `await` reused coroutines. # And they have to be fresh. That's why we use subtests for it. FutureResult.from_value(1), FutureResult.from_failure(1), FutureResult.from_io(IO(1)), FutureResult.from_failed_io(IO(1)), FutureResult.from_ioresult(IOSuccess(1)), FutureResult.from_ioresult(IOFailure(1)), FutureResult.from_result(Success(1)), FutureResult.from_result(Failure(1)), FutureResult.from_future(Future.from_value(1)), FutureResult.from_failed_future(Future.from_value(1)), FutureResult.from_typecast(Future.from_value(Success(1))), ] for container in containers: with subtests.test(container=container): result_inst = await container assert result_inst._inner_value._inner_value == 1 # noqa: WPS437
def test_regression394(): """ It used to raise ``ImmutableStateError`` for type aliases. Here we use the minimal reproduction sample. .. code:: python Traceback (most recent call last): File "ex.py", line 18, in <module> get_ip_addr("https://google.com") File "ex.py", line 13, in get_ip_addr return RequiresContextIOResultE(lambda _: IOSuccess(1)) File "../3.7.7/lib/python3.7/typing.py", line 677, in __call__ result.__orig_class__ = self File "../returns/returns/primitives/types.py", line 42, in __setattr__ raise ImmutableStateError() returns.primitives.exceptions.ImmutableStateError See: https://github.com/dry-python/returns/issues/394 """ assert RequiresContextIOResultE(lambda _: IOSuccess(1))
def test_bind_with_context_io_result(): """Ensures that functions can be composed and return type is correct.""" binded = bind(_context_io_result_function) assert binded(RequiresContextIOResult.from_success(3))(5) == IOSuccess(8)
def test_ioresult_value_or(): """Ensures that ``value_or`` works correctly.""" assert IOSuccess(1).value_or(0) == IO(1) assert IOFailure(1).value_or(0) == IO(0)
def test_failure_iosuccess(): """Ensures that failure works for IOSuccess container.""" with pytest.raises(UnwrapFailedError): IOSuccess(5).failure()
def test_unwrap_iosuccess(): """Ensures that unwrap works for IOSuccess container.""" assert IOSuccess(5).unwrap() == IO(5)
RequiresContextIOResult, RequiresContextResult, ) from returns.converters import flatten from returns.io import IO, IOFailure, IOSuccess from returns.maybe import Nothing, Some from returns.result import Failure, Success @pytest.mark.parametrize( ('container', 'merged'), [ # Flattens: (IO(IO(1)), IO(1)), (Success(Success({})), Success({})), (IOSuccess(IOSuccess(1)), IOSuccess(1)), (Some(Some(None)), Nothing), (Some(Some([])), Some([])), # Nope: (Failure(Failure('a')), Failure(Failure('a'))), (IOFailure(IOFailure('a')), IOFailure(IOFailure('a'))), ]) def test_flatten(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container) == merged @pytest.mark.parametrize(('container', 'merged'), [ ( RequiresContextResult.from_success(
self, inner_value: str, use_result: Result[str, str], ) -> ReaderIOResult[None, str, NoDeps]: return ReaderIOResult.from_failure('release failure') @pytest.mark.parametrize( ('acquire', 'use', 'release', 'final_result', 'log'), [ # Acquire success: ( _acquire_success, _use_success, _ReleaseSuccess, IOSuccess('use success'), [('acquire success', Success('use success'))], ), ( _acquire_success, _use_success, _ReleaseFailure, IOFailure('release failure'), [], ), ( _acquire_success, _use_failure, _ReleaseSuccess, IOFailure('use failure'), [('acquire success', Failure('use failure'))],
def _context_io_result_function( argument: int, ) -> RequiresContextIOResult[int, int, str]: return RequiresContextIOResult(lambda other: IOSuccess(argument + other))
def _ioresult_function(argument: int) -> IOResult[str, str]: return IOSuccess(str(argument + 1))