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.
pip install safe-returnsfrom 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