Concepts 🚀 Project 🧮 Labs
📚 Learn / Python / Decorators

Decorators

A function that wraps another function. @router.get('/x') is a decorator that registers the function as a route handler.

Code example

@router.get('/health')
async def health():
    return {'status': 'ok'}

# equivalent to:
async def health():
    return {'status': 'ok'}
health = router.get('/health')(health)

How it works

A decorator is a function that takes a function and returns a (usually wrapped) function. The `@syntax` is sugar: `@dec def f(): ...` is `f = dec(f)`. FastAPI's `@router.get('/path')` is a decorator that registers the function as the handler for `GET /path`. The auth middleware uses a `@require_login` decorator on routes that need a logged-in user.

🔍 Where this is used in OpenBenchML Every single route in app/routes/. Also `@app.middleware('http')` in main.py.