def test_result_error_iterate(): with pytest.raises(Error) as excinfo: # type: ignore error: Error[int, str] = Error("err") for _ in error: assert False assert excinfo.value.error == "err" # type: ignore
def test_result_match_error(): xs: Result[int, str] = Error("err") with match(xs) as case: for err in case(Error[int, str]): assert err == "err" break else: assert False
def test_result_traverse_error(xs: List[int]): error = "Do'h" ys: List[Result[int, str]] = [ Ok(x) if i == 3 else Error(error) for x, i in enumerate(xs) ] zs = sequence(ys) for err in zs.match(Error[int, str]): assert err == error
def test_result_error_chained_map(msg: str, y: int): xs: Result[int, str] = Error(msg) mapper1: Callable[[int], int] = lambda x: x + y mapper2: Callable[[int], int] = lambda x: x * 10 ys = xs.map(mapper1).map(mapper2) with pytest.raises(CustomException) as ex: ys.match(Ok, lambda ok: ok.value, Error, lambda error: throw(CustomException(error.error))) assert ex.value.message == msg
def wrapper(*args: Any, **kwargs: Any) -> Result[_TSource, _TError]: try: out = fn(*args, **kwargs) except exception as exn: return Error(cast(_TError, exn)) else: if isinstance(out, Result): return cast(Result[_TSource, _TError], out) return Ok(out)
def test_result_map_error_fluent(msg: str, y: int): xs: Result[int, str] = Error(msg) mapper: Callable[[int], int] = lambda x: x + y ys = xs.map(mapper) for err in ys.match(Error[int, str]): assert err == msg break else: assert False
def test_result_traverse_error(xs: List[int]): error = "Do'h" ys: List[Result[int, str]] = [ Ok(x) if i == 3 else Error(error) for x, i in enumerate(xs) ] zs = sequence(ys) res = zs.match(Ok, lambda ok: "", Error, lambda error: error.error) assert res == error
def test_pipeline_error(): error: Result[int, str] = Error("failed") fn: Callable[[int], Result[int, str]] = lambda x: Ok(x * 10) gn: Callable[[int], Result[int, str]] = lambda x: error hn = pipeline( fn, gn, ) assert hn(42) == error
def test_result_error_chained_map(msg: str, y: int): xs: Result[int, str] = Error(msg) mapper1: Callable[[int], int] = lambda x: x + y mapper2: Callable[[int], int] = lambda x: x * 10 ys = xs.map(mapper1).map(mapper2) for err in ys.match(Error[int, str]): assert err == msg break else: assert False
def test_result_error(): error = CustomException("d'oh!") xs: Result[str, Exception] = Error(error) assert isinstance(xs, Result) assert not xs.is_ok() assert xs.is_error() assert str(xs) == f"Error {error}" with pytest.raises(CustomException): match(xs, Ok, lambda ok: ok.value, Error, lambda error: throw(error.error))
def test_result_map_error_fluent(msg: str, y: int): xs: Result[int, str] = Error(msg) mapper: Callable[[int], int] = lambda x: x + y ys = xs.map(mapper) with pytest.raises(CustomException) as ex: match( ys, Ok, lambda ok: ok.value, Error, lambda error: throw(CustomException(error.error)), ) assert ex.value.message == msg
def test_result_error(): error = CustomException("d'oh!") xs: Result[str, Exception] = Error(error) assert isinstance(xs, Result) assert not xs.is_ok() assert xs.is_error() assert str(xs) == f"Error {error}" for _ in xs.match(Ok[str, Exception]): assert False for ex in xs.match(Error[str, Exception]): assert ex == error
async def fetch( next: HttpFunc[Option[ClientResponse], TResult, TError], ctx: HttpContext, ) -> Result[Context[TResult], TError]: session = ctx.Request.SessionFactory() builder: Callable[[Any], HttpContent] = lambda builder: builder() result: HttpFuncResult try: content: Option[Any] = pipe(ctx.Request.ContentBuilder, option.map(builder)) json = pipe(content, option.default_value(None)) method = ctx.Request.Method.value url = ctx.Request.UrlBuilder(ctx) print(f"Fetching: {url}") async with session.request(method=method, url=url, json=json) as resp: result = await next(ctx.replace(Response=Some(resp))) except Exception as ex: print(f"fetch: {ex}") result = Error(ex) return result
def fn(a: int) -> Generator[int, Any, int]: b = yield from Error(ValueError("failure")) return a + b
def test_result_error_equals_error(x: int, y: int): xs: Result[int, int] = Error(x) ys: Result[int, int] = Error(y) assert xs == ys if x == y else xs != ys
def test_result_ok_not_equals_error(x: int): assert not Ok(x) == Error(x) assert not Error(x) == Ok(x)
def test_result_error_iterate(): with pytest.raises(Error) as excinfo: for x in Error("err"): assert x == 42 assert excinfo.value.error == "err"
def mayfail() -> Error[int, str]: return Error(error)
def fn() -> Generator[int, int, int]: x = yield from Error(error) return x