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 / sub-dependency that declares a parameter of this type receives the union of all Security(..., scopes=[...]) calls between the route entry and this point in the dependency graph. Typical use: an authorising dependency checks security_scopes.scopes against the scopes the bearer token actually carries and builds a WWW-Authenticate: Bearer scope="<...>" header when denying.

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