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.
pip install saferaiseRequires Python 3.13+
# 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.
Full documentation is available at https://paillat.dev/saferaise
MIT License. See LICENSE for details.