{"id":7160,"library":"dishka","title":"Dishka","description":"Dishka is a lightweight and performant dependency injection framework for Python, designed to be framework-agnostic and support asynchronous operations and scopes. It leverages type hints for dependency resolution, aiming for a 'cute' and agreeable API. The current version is 1.9.1, and it maintains an active release cadence with frequent minor updates and bug fixes.","status":"active","version":"1.9.1","language":"en","source_language":"en","source_url":"https://github.com/reagento/dishka","tags":["dependency-injection","di","async","framework-agnostic","type-hints"],"install":[{"cmd":"pip install dishka","lang":"bash","label":"Core library"},{"cmd":"pip install dishka[fastapi]","lang":"bash","label":"For FastAPI integration"},{"cmd":"pip install dishka[aiogram]","lang":"bash","label":"For AioGram integration"},{"cmd":"pip install dishka[litestar]","lang":"bash","label":"For Litestar integration"}],"dependencies":[{"reason":"Optional integration with FastAPI","package":"fastapi","optional":true},{"reason":"Optional integration with AioGram","package":"aiogram","optional":true},{"reason":"Optional integration with Litestar","package":"litestar","optional":true},{"reason":"Optional integration with FastStream","package":"faststream","optional":true},{"reason":"Optional integration with Starlite","package":"starlite","optional":true},{"reason":"Optional integration with Taskiq","package":"taskiq","optional":true},{"reason":"Optional integration with ARQ","package":"arq","optional":true}],"imports":[{"symbol":"Provider","correct":"from dishka import Provider"},{"symbol":"Scope","correct":"from dishka import Scope"},{"symbol":"provide","correct":"from dishka import provide"},{"symbol":"inject","correct":"from dishka import inject"},{"symbol":"make_container","correct":"from dishka import make_container"}],"quickstart":{"code":"import asyncio\nfrom dishka import Provider, Scope, provide, make_container\n\nclass MyDependency:\n    def __init__(self, name: str):\n        self.name = name\n\nclass MyService:\n    def __init__(self, dep: MyDependency):\n        self.dep = dep\n\nclass MyProvider(Provider):\n    @provide(scope=Scope.APP)\n    def get_dependency(self) -> MyDependency:\n        return MyDependency(name=\"Global Dep\")\n\n    @provide(scope=Scope.REQUEST)\n    def get_service(self, dep: MyDependency) -> MyService:\n        return MyService(dep)\n\nasync def main():\n    # 1. Create a container with your providers\n    container = make_container(MyProvider())\n\n    # 2. Enter an application scope\n    async with container(scope=Scope.APP) as app_container:\n        # 3. Enter a request scope (or other child scope)\n        async with app_container(scope=Scope.REQUEST) as request_container:\n            # 4. Get a dependency from the current scope\n            service = await request_container.get(MyService)\n            print(f\"Service created with dependency name: {service.dep.name}\")\n\n    await container.close()\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates how to define dependencies using `Provider` and `provide`, manage scopes (`Scope.APP`, `Scope.REQUEST`) with `make_container`, and retrieve services. Dependencies are automatically resolved based on type hints."},"warnings":[{"fix":"Ensure you install Dishka with the correct extra, e.g., `pip install dishka[fastapi]`.","message":"Dishka's framework integrations (e.g., FastAPI, AioGram) require installing extra packages (e.g., `dishka[fastapi]`). Forgetting these will result in `ModuleNotFoundError` for integration-specific imports.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Carefully manage the lifetime of your `Container` and its nested scopes using `async with` blocks. Always resolve dependencies from the appropriate scope context.","message":"Incorrect scope management, such as trying to exit a scope that isn't the current active one or accessing a shorter-lived dependency from a longer-lived scope, will lead to `ScopeExitError` or `InvalidDependencyGraphError`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all dependencies have clear, resolvable type hints. Refer to Dishka's official documentation for best practices regarding advanced type hinting constructs like `Annotated`.","message":"Dishka relies heavily on accurate type hints for dependency resolution. Complex or ambiguous type hints (e.g., `Union`, `Annotated`, `Protocol`) can sometimes lead to `NotAFactoryError` or `UnsupportedAnnotationError` if not handled precisely.","severity":"gotcha","affected_versions":">=1.5.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure your dependency provider class inherits from `dishka.Provider`, e.g., `class MyProvider(Provider):`.","cause":"A class intended to define dependencies does not inherit from `dishka.Provider`.","error":"TypeError: object is not a Provider"},{"fix":"Verify that all types required by `MyService` (and `MyService` itself) are provided by a `@provide` decorated method in your `Provider`s, and check for circular dependencies.","cause":"The requested type (e.g., `MyService`) or one of its dependencies cannot be found in the registered providers, or there's a circular dependency.","error":"dishka.exceptions.InvalidDependencyGraphError: Cannot find dependency for MyService"},{"fix":"Install Dishka with the required extra: `pip install dishka[fastapi]` (replace `fastapi` with your desired integration).","cause":"You are attempting to use a Dishka integration (e.g., for FastAPI) without installing the necessary extra package.","error":"ModuleNotFoundError: No module named 'dishka.integrations.fastapi'"},{"fix":"Ensure that container scopes are entered and exited in the correct LIFO (Last-In, First-Out) order, typically by nesting `async with container(scope=...)` blocks correctly.","cause":"You are trying to exit a container scope that is not the most recently entered or active scope, often due to mismatched `async with` blocks.","error":"dishka.exceptions.ScopeExitError: Cannot exit scope REQUEST: it is not the current active scope."}]}