Skip to content

Dependency Injection

The markers that declare what a handler needs and how it is authorised.

Depends

Dependency marker — use in function signature defaults.

dependency may be omitted (Depends()); the resolver then infers it from the parameter's type annotation — the shorthand for x: SomeClass = Depends().

Usage::

def get_db() -> Database:
    return Database()

@app.get("/users")
async def list_users(db: Database = Depends(get_db)) -> list[str]:
    return db.all_usernames()

Security

Bases: Depends

Dependency marker with OAuth2 scopes for OpenAPI emission.

SecurityScopes

Aggregated OAuth 2.0 scopes for the current Security() chain.

A handler or sub-dependency declaring a parameter of this type receives the union of every Security(..., scopes=[...]) between the route entry and this point in the graph. An authorising dependency checks security_scopes.scopes against what the bearer token carries and builds a WWW-Authenticate: Bearer scope="<...>" header when denying.

Per RFC 6749 Sec. 3.3 the scope-string serialisation is space-separated.