Skip to content

⚙️ Result monad

This container wraps a result computation value into Ok or when the function call failed, returns Err(exception)

💻 API reference

ResultShortcutError

Bases: Exception, t.Generic[T_error]

Parameters:

Name Type Description Default
Exception _type_

description

required
t _type_

description

required
Source code in fateful/monad/result.py
258
259
260
261
262
263
264
265
266
267
class ResultShortcutError(Exception, t.Generic[T_error]):
    """
    Args:
        Exception (_type_): _description_
        t (_type_): _description_
    """

    def __init__(self, error: T_error):
        super().__init__(error)
        self.error = error

result_shortcut(f)

summary

Parameters:

Name Type Description Default
f t.Callable[P_mapper, V]

description

required

Returns:

Type Description
t.Callable[P, Result[T_co, T_error]]

t.Callable[P_mapper, Ok[V] | Err[t.Any]]: description

Source code in fateful/monad/result.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def result_shortcut(
    f: t.Callable[P, Result[T_co, T_error]]
) -> t.Callable[P, Result[T_co, T_error]]:
    """
    _summary_

    Args:
        f (t.Callable[P_mapper, V]): _description_

    Returns:
        t.Callable[P_mapper, Ok[V] | Err[t.Any]]: _description_
    """

    def inner(*args: P.args, **kwargs: P.kwargs) -> Ok[T_co] | Err[T_error]:
        try:
            return Ok(f(*args, **kwargs))  # type: ignore[arg-type]
        except Exception as e:
            if isinstance(e, ResultShortcutError):
                return Err(e.error)
            raise

    return inner

sync_try(f, exc=(Exception))

Run a function that may raise an exception and return a Result type.

Parameters:

Name Type Description Default
exc tuple[type[T_err], ...]

description. Defaults to (Exception,)

(Exception)
Source code in fateful/monad/result.py
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def sync_try(
    f: t.Callable[P, T_co],
    exc: type[T_error] | tuple[type[T_error], ...] = (Exception,),  # type: ignore
) -> t.Callable[P, Result[T_co, T_error]]:
    """
    Run a function that may raise an exception and return a Result type.
    Args:
        exc (tuple[type[T_err], ...], optional): _description_. Defaults to (Exception,)
    """

    def inner(*args: P.args, **kwargs: P.kwargs) -> Result[T_co, T_error]:
        try:
            return Ok(f(*args, **kwargs))
        except Exception as e:
            errors = exc if isinstance(exc, tuple) else (exc,)
            for err in errors:
                if isinstance(e, err):
                    return Err(e)
            raise

    return inner

to_result(exc=(Exception))

Decorator to convert a function that may raise an exception to a Result type.

Parameters:

Name Type Description Default
exc tuple[type[T_err], ...]

description. Defaults to (Exception,)

(Exception)

Returns:

Name Type Description
_type_ t.Callable[[t.Callable[P, T_co]], t.Callable[P, Result[T_co, T_error]]]

description

Source code in fateful/monad/result.py
243
244
245
246
247
248
249
250
251
252
253
254
255
def to_result(
    exc: type[T_error] | tuple[type[T_error], ...] = (Exception,)  # type: ignore
) -> t.Callable[[t.Callable[P, T_co]], t.Callable[P, Result[T_co, T_error]]]:
    """
    Decorator to convert a function that may raise an exception to a Result type.

    Args:
        exc (tuple[type[T_err], ...], optional): _description_. Defaults to (Exception,)

    Returns:
        _type_: _description_
    """
    return functools.partial(sync_try, exc=exc)  # type: ignore