Skip to content

Dependency injection

The built-in container, parameter injection and the adapters. See the Dependency injection guide.

Container.singleton()

def singleton(key: Factory[T], factory: Factory[T] | None = None) -> None: ...

One instance per container, built from factory (defaults to key).

Parameter Type Default Description
key Factory[T] — What is requested (a class, abstract class, protocol or generic alias).
factory Factory[T] \| None None Builds it: a class, function, generator or async variant (default: key itself).

Container.scoped()

def scoped(key: Factory[T], factory: Factory[T] | None = None) -> None: ...

One instance per scope; unavailable outside of one.

Parameter Type Default Description
key Factory[T] — What is requested.
factory Factory[T] \| None None Builds it once per scope (request, or container.scope()); generators clean up when the scope closes.

Container.transient()

def transient(key: Factory[T], factory: Factory[T] | None = None) -> None: ...

A new instance on every resolution.

Parameter Type Default Description
key Factory[T] — What is requested.
factory Factory[T] \| None None Builds a new value every time it is resolved.

Container.register()

def register(key: Factory[T], factory: Factory[T] | None = None, *, lifetime: Lifetime) -> None: ...
Parameter Type Default Description
key Factory[T] — What is requested.
factory Factory[T] \| None None Builds it (default: key itself).
lifetime Lifetime — Lifetime.SINGLETON, SCOPED or TRANSIENT.

Container.instance()

def instance(key: Factory[object], value: object) -> None: ...

Register an already constructed object.

Parameter Type Default Description
key Factory[object] — What is requested.
value object — The object returned; checked against class keys at registration.

Container.override()

def override(key: Factory[object], value: object) -> Generator[None]: ...

Temporarily replace a dependency, e.g. with a fake in tests.

Parameter Type Default Description
key Factory[object] — The dependency to replace.
value object — The replacement, until the with block ends.

Container.resolve()

def resolve(key: Factory[T]) -> T: ...

Resolve outside of a scope (scoped dependencies are rejected).

Parameter Type Default Description
key Factory[T] — What to build (sync factories only).

Container.aresolve()

def aresolve(key: Factory[T]) -> T: ...
Parameter Type Default Description
key Factory[T] — What to build (sync and async factories).

Container.scope()

def scope(values: Mapping[object, object] | None = None) -> Scope: ...

A scope for scoped services, closed (with cleanups) when the block exits.

Parameter Type Default Description
values Mapping[object, object] \| None None Values available to scoped factories, e.g. {RequestContext[User, None]: context}.

Container.check()

def check(key: Factory[object], /, *, asynchronous: bool = False) -> None: ...

Validate the graph of key without building anything.

Parameter Type Default Description
key Factory[object] — What will be requested.
asynchronous bool False Whether async operations will request it (allows async factories).

Container.close()

def close() -> None: ...

Run the cleanup of generator singletons.

Container.aclose()

def aclose() -> None: ...

Parameter injection

Annotation Value
Inject[T] T resolved from the controller's container for this call
Annotated[T, Resolve(fn)] fn(request) (sync, or async def in async operations)

request_context()

def request_context(user_type: type[UserT], tenant: Callable[[HttpRequest, UserT], TenantT] | None = None) -> Callable[[HttpRequest], RequestContext[UserT, TenantT]] | Callable[[HttpRequest], RequestContext[UserT, None]]: ...

A scoped factory building RequestContext from the request.

Parameter Type Default Description
user_type type[UserT] — The user class; the authenticated user must be one, else 401.
tenant Callable[[HttpRequest, UserT], TenantT] \| None None Returns the tenant from (request, user); None for single-tenant apps.

authenticated_user()

def authenticated_user(user_type: type[UserT]) -> Callable[[HttpRequest], UserT]: ...

A scoped factory: container.scoped(User, authenticated_user(User)).

Parameter Type Default Description
user_type type[UserT] — The user class; the authenticated user must be one, else 401.

DishkaResolver

Sync operations use container; async ones async_container when given.

Name Type Default Description
container Container — The sync dishka container.
scope BaseScope Scope.REQUEST dishka scope opened per request.
async_container AsyncContainer \| None None Async container for async operations (optional).

provide_controllers()

def provide_controllers(provider: Provider, controllers: Iterable[type[object]], *, scope: BaseScope = Scope.REQUEST) -> None: ...

Register controller classes with provider (their __init__ is autowired).

Parameter Type Default Description
provider Provider — The dishka provider to register on.
controllers Iterable[type[object]] — Controller classes (their __init__ is autowired).
scope BaseScope Scope.REQUEST dishka scope of the controllers.