Skip to main content

Starlette integration for Dishka

PyPI version Supported versions Downloads License GitHub Actions Workflow Status Telegram

This package provides integration of Dishka dependency injection framework and Starlette, a lightweight ASGI framework.

Installation

pip install starlette-dishka

Features

  • automatic REQUEST and SESSION scope management using middleware
  • passing Request object as a context data to providers for both Websockets and HTTP requests
  • automatic injection of dependencies into handler function.

How to use

  1. Import
from starlette_dishka import (
    FromDishka,
    StarletteProvider,
    inject,
    setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
  1. Create provider. You can use starlette.requests.Request as a factory parameter to access on REQUEST-scope, and starlette.websockets.WebSocket on SESSION-scope
class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_x(self, request: Request) -> X:
         ...
  1. Mark those of your handlers parameters which are to be injected with FromDishka[] and decorate them using @inject
@inject
async def endpoint(
    request: Request,
    *,
    gateway: FromDishka[Gateway],
) -> ResponseModel:
    ...
  1. (optional) Use StarletteProvider() when creating container if you are going to use starlette.Request or starlette.WebSocket in providers
container = make_async_container(YourProvider(), StarletteProvider())
  1. Setup dishka integration.
setup_dishka(container=container, app=app)

Websockets

In starlette your view function is called once per connection and then you retrieve messages in loop. So, inject decorator can be only used to retrieve SESSION-scoped objects. To achieve REQUEST-scope you can enter in manually:

@inject
async def get_with_request(
    websocket: WebSocket,
    a: FromDishka[A],  # object with Scope.SESSION
    container: FromDishka[AsyncContainer],  # container for Scope.SESSION
) -> None:
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        # enter the nested scope, which is Scope.REQUEST
        async with container() as request_container:
            b = await request_container.get(B)  # object with Scope.REQUEST

Release files for starlette-dishka 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-dishka 1.0.2
File Size Uploaded
starlette_dishka-1.0.2.tar.gz 16.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-dishka 1.0.2
File Interpreter ABI Platform
starlette_dishka-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.9 kB

Release files / starlette_dishka-1.0.2.tar.gz

Download URL starlette_dishka-1.0.2.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f01054d9ca675e7778283b66bffaa5284a7e6e8cb3d895d103aba3f117dd715a
BLAKE2b-256 checksum
How to use checksums
91324da097f2443ceafae09d418fcc62e7e9f0e637b8c37c90ac36f68b17e2a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / starlette_dishka-1.0.2-py3-none-any.whl

Download URL starlette_dishka-1.0.2-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e554ebc99c7a494c1084196871ccdb3f313f8430347d589576d688b5645fbbfe
BLAKE2b-256 checksum
How to use checksums
c945aca50b953a81968a7e68aa1b764d0c86fe061ccf8b7779b108aca3d72f35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page