Starlette integration for Dishka
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
Requestobject as a context data to providers for both Websockets and HTTP requests - automatic injection of dependencies into handler function.
How to use
- Import
from starlette_dishka import (
FromDishka,
StarletteProvider,
inject,
setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
- Create provider. You can use
starlette.requests.Requestas a factory parameter to access on REQUEST-scope, andstarlette.websockets.WebSocketon SESSION-scope
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, request: Request) -> X:
...
- 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:
...
- (optional) Use
StarletteProvider()when creating container if you are going to usestarlette.Requestorstarlette.WebSocketin providers
container = make_async_container(YourProvider(), StarletteProvider())
- Setup
dishkaintegration.
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)
| File | Size | Uploaded | |
|---|---|---|---|
| starlette_dishka-1.0.2.tar.gz | 16.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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}
|