oapi-gen-dishka
Inject Dishka dependencies into the async methods called by an oapi-gen router.
Generated code stays unchanged. The integration reuses Dishka's native Starlette
request container, so operation handlers, security handlers, and ordinary Starlette
endpoints share the same Scope.REQUEST instances.
Use the same version of oapi-gen-dishka as the oapi-gen generator that produced
your application code. Both packages are published together; see
the release instructions.
uv add oapi-gen-dishka
This directory is also independently buildable. To install from a local checkout:
uv add /path/to/oapi-gen/packages/oapi-gen-dishka
Usage
The example uses the repository's complete Cats specification. Generate it into your application's package:
oapi-gen generate packages/oapi-gen/tests/fixtures/cats.openapi.yaml --output app/http/generated
from contextlib import asynccontextmanager
from uuid import UUID, uuid4
from dishka import FromDishka, Provider, Scope, make_async_container, provide
from starlette.applications import Starlette
from oapi_gen_dishka import inject, setup_dishka
from app.http.generated import Handlers, contracts, create_router, models
class CatsRepository:
def __init__(self):
self.cats: dict[UUID, models.Cat] = {}
class CatsController:
@inject
async def list_cats(
self,
request: contracts.ListCats.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.ListCats.Response:
cats = list(repository.cats.values())
return contracts.ListCats.Ok(body=cats[: request.limit])
@inject
async def create_cat(
self,
request: contracts.CreateCat.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.CreateCat.Response:
cat = models.Cat(id=uuid4(), name=request.body.name)
repository.cats[cat.id] = cat
return contracts.CreateCat.Created()
@inject
async def show_cat_by_id(
self,
request: contracts.ShowCatById.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.ShowCatById.Response:
cat = repository.cats.get(request.cat_id)
if cat is None:
raise contracts.ShowCatById.NotFound(
body=models.HttpError(message="Cat not found"),
)
return contracts.ShowCatById.Ok(body=cat)
class AppProvider(Provider):
# This in-memory example keeps cats between requests in one process.
repository = provide(CatsRepository, scope=Scope.APP)
container = make_async_container(AppProvider())
@asynccontextmanager
async def lifespan(app: Starlette):
try:
yield
finally:
await container.close()
router = create_router(Handlers(cats=CatsController()), prefix="/api")
app = Starlette(lifespan=lifespan, routes=router.routes)
setup_dishka(container, app)
Use Scope.REQUEST for services or database sessions that must be recreated for
each request. Dishka resolves dependencies from the normal provider graph and
finalizes generator providers at the end of the request, including on exceptions.
Add Dishka's StarletteProvider() from dishka.integrations.starlette when a
provider needs starlette.requests.Request.
@inject also works on generated security handler methods. Use
dishka.integrations.starlette.inject on ordinary Starlette endpoints; both
decorators resolve from the same container after the single setup call above.
Boundaries
-
Async HTTP handlers only. WebSockets, sync containers, and background work outside the request lifetime are not supported by this decorator.
-
Call this package's
setup_dishkaonce, before startup, instead of callingdishka.integrations.starlette.setup_dishkaseparately. -
The controller instance passed to
Handlersis shared. Keep request-specific state in local variables and injected dependencies, not onself. -
Providers must register dependencies;
FromDishka[T]selects the registered type and does not register it automatically. Dishka components are supported. -
Runtime signatures omit injected parameters and retain the remaining annotations. The decorator preserves the static response type but uses
Callable[..., ...]for arguments, since Python typing cannot remove arbitraryFromDishkaparameters. Annotate controller references with the generated protocol when calling them directly to check request argument types:controller: contracts.CatsHandler = CatsController()
Development
uv sync --all-packages
uv run --all-packages --directory packages/oapi-gen-dishka pytest
uv run --all-packages --directory packages/oapi-gen-dishka ruff check .
uv run --all-packages --directory packages/oapi-gen-dishka basedpyright
uv build --package oapi-gen-dishka --out-dir dist
Run these commands from the repository root; the workspace uses one root lockfile.
The workspace dependency on oapi-gen is used only for integration tests;
the published wheel does not depend on the generator at runtime.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oapi_gen_dishka-0.1.9.tar.gz.
File metadata
- Download URL: oapi_gen_dishka-0.1.9.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f88ea34528d34f8862cbdae204beb770ccf467984c0aefc89bf6e727d7bcb8e5
|
|
| MD5 |
82cabb0a5d5a64dd4527c911c8c21712
|
|
| BLAKE2b-256 |
49c1c9b8f17e1740ba75c545a1cad633ac2ca042cce0e4190965ffc4583c9fe4
|
Provenance
The following attestation bundles were made for oapi_gen_dishka-0.1.9.tar.gz:
Publisher:
release.yml on mishamyrt/oapi-gen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oapi_gen_dishka-0.1.9.tar.gz -
Subject digest:
f88ea34528d34f8862cbdae204beb770ccf467984c0aefc89bf6e727d7bcb8e5 - Sigstore transparency entry: 2820691073
- Sigstore integration time:
-
Permalink:
mishamyrt/oapi-gen@1bc8a1e6fd2a0c4ef0c87f266dd09c78da546574 -
Branch / Tag:
refs/tags/v0.1.9 - Owner: https://github.com/mishamyrt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1bc8a1e6fd2a0c4ef0c87f266dd09c78da546574 -
Trigger Event:
push
-
Statement type:
File details
Details for the file oapi_gen_dishka-0.1.9-py3-none-any.whl.
File metadata
- Download URL: oapi_gen_dishka-0.1.9-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e8b49b76f7c09ba5c514265c0c7198d790d9044710526d76ef9f5f703209283
|
|
| MD5 |
2f059fbd5077765fee017bf7d8b51f8d
|
|
| BLAKE2b-256 |
670eadc278c1c19d1d0675100c3fd510da143f3d65cfed030b7faf939e254971
|
Provenance
The following attestation bundles were made for oapi_gen_dishka-0.1.9-py3-none-any.whl:
Publisher:
release.yml on mishamyrt/oapi-gen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oapi_gen_dishka-0.1.9-py3-none-any.whl -
Subject digest:
0e8b49b76f7c09ba5c514265c0c7198d790d9044710526d76ef9f5f703209283 - Sigstore transparency entry: 2820691154
- Sigstore integration time:
-
Permalink:
mishamyrt/oapi-gen@1bc8a1e6fd2a0c4ef0c87f266dd09c78da546574 -
Branch / Tag:
refs/tags/v0.1.9 - Owner: https://github.com/mishamyrt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1bc8a1e6fd2a0c4ef0c87f266dd09c78da546574 -
Trigger Event:
push
-
Statement type: