Larz โ€บ Dependency injection in Python

Dependency injection, the simple way

Share database handles, current-user lookups and config across handlers without globals โ€” resolved and cached per request.

pip install larz
def current_user(req):
    return db.get(req.session.get("user"))

@app.get("/me")
def me(req, user=Depends(current_user)):   # injected & cached for this request
    return user.email

QuickstartGitHubDocs

How it works

A dependency is just a callable. If it takes an argument, Larz passes the request; otherwise it calls it with none. Each dependency is resolved once per request and cached, so two handlers (or a handler and its sub-dependencies) sharing a dependency get the same instance. It composes with typed parameters and validation.

FAQ

Is it like FastAPI's Depends?

Yes โ€” same idea and ergonomics: Depends(fn) injects the result of a callable, cached per request. Larz implements it with the standard library, no dependencies.

Can dependencies use the request?

Yes โ€” if the dependency callable takes an argument, Larz passes the current request to it.