Skip to content

Repository files navigation

safe-returns

A decorator for converting the output type of a function into an algebraic data type, representing the function’s result and its possible exception types. This helps in tracking exception types and improves type-checker hints.

PyPI - Python Version PyPI - Status PyPI - Implementation PyPI - Downloads

Pre-commit uv Ruff Checked with pyright Semantic Versions

Test results Coverage Status

Documentation GitHub License GitHub Release GitHub Repo stars

Install

pypi

pip install safe-returns

Uses

from safe import safe, Success, Failure

@safe @ ValueError | KeyError
def foo() -> int | str: ...


match foo():
    case Success(value=int() as number):
        print(f"It's int {number=}")
    case Success(value=str() as string):
        print(f"It's str {string=}")
    case Failure(error=ValueError()):
        print("Catch ValueError")
    # reportMatchNotExhaustive warning – KeyError are not handled

Documentation

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages