Skip to content

Paillat-dev/saferaise

Repository files navigation

saferaise

Alpha PyPI py.typed CI License: MIT

Checked exceptions for Python. Declare what your functions raise, and let saferaise enforce it - using your existing try/except blocks.

Caution

Alpha: The API may change between releases, and using this right now might very well break your code in weird ways, so don't use it in production.

Installation

pip install saferaise

Requires Python 3.13+

Quick Start

# entrypoint.py
import saferaise

saferaise.register("app")   # instrument app's try/except blocks before importing it

import app
with saferaise.enable():    # activate checking for this scope
    app.main()
# app.py
from saferaise import raises

@raises(ValueError)
def parse_input(raw: str) -> int:
    return int(raw)

def main():
    try:
        parse_input("abc")  # ValueError is caught here - @raises is satisfied
    except ValueError:
        print("bad input")

register must be called before importing the package it instruments, and in a separate file from the code being instrumented.

Documentation

Full documentation is available at https://paillat.dev/saferaise

License

MIT License. See LICENSE for details.

About

Checked exceptions for Python. Declare what your functions raise, and let saferaise enforce it - using your existing try/except blocks.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors

Languages