Skip to main content Switch to mobile version nuke-di 3.0.2 Simplest type-hint based dependency injection for async Python projects
pip install nuke-di Copy PIP instructions
The simplest dependency injection for async Python projects.
Dependencies are declared with plain type hints. nuke-di builds the dependency tree, creates every client once and drives its async lifecycle: connect() on startup and disconnect() on shutdown. Every client starts as soon as its own dependencies have connected, concurrently with every other client that is ready.
FastAPI, Litestar, FastStream and aiogram handlers, MCP tools and taskiq tasks take clients by type hint. An optional @entrypoint runs an async function when its module is executed.
It was extracted from the DI layer of a production Python microservice framework and has no runtime dependencies.
Installation · Quick start · Principles · Performance
Examples: an executable module · FastAPI
pip install nuke-di Requires Python 3.11+.
import asyncio from nuke_di import DI, Client class Database(Client): async def connect(self) -> None: print("database: connected") async def disconnect(self) -> None: print("database: disconnected") async def fetch_user(self, user_id: int) -> str: return f"user-{user_id}" class UserService(Client): def __init__(self, db: Database) -> None: self._db = db async def greet(self, user_id: int) -> str: name = await self._db.fetch_user(user_id) return f"Hello, {name}!" async def handler(user_id: int, users: UserService) -> str: return await users.greet(user_id) async def main() -> None: injected = DI.inject(handler) # resolves UserService -> Database async with DI: # connect() every client, disconnect() on exit print(await injected(42)) asyncio.run(main()) database: connected Hello, user-42! database: disconnected What happened:
DI.inject(handler) read the type hints of handler, found the client UserService, saw that it needs a Database in its __init__ and built both. user_id: int is not a client, so it stays a regular argument.
async with DI called connect() on every client it built, dependencies first.
injected(42) called handler(42, users= ).
Leaving the async with block called disconnect() in reverse order.
A dependency is a class. A subclass of Client with a type-hinted __init__ and async connect() / disconnect() is the whole model: no providers, no modules, no registration, no scopes to configure. A third-party object becomes a dependency by wrapping it in such a class.
Type hints are the wiring. A client asks for its dependencies in __init__, a function in its signature. Nothing else names them, so renaming or adding a dependency is an ordinary refactoring.
Concurrent startup, ordered shutdown. A client connects as soon as its own dependencies have, concurrently with every other client that is ready, so a slow client holds back only the clients that need it. They disconnect in reverse, and a failing disconnect() does not stop the others.
Fail fast. A tree that cannot be built fails before anything connects, naming the argument and the path to it, and mypy with the plugin reports the same error before the process starts. A client that cannot connect stops the application once the connected ones are disconnected. There are no retries: restarting is the orchestrator's job.
Tests replace, they do not rewire. mock() and override() put a fake in place of a client for one test; the code under test does not change.
No runtime dependencies. The core uses only the standard library; the framework integrations are extras.
How a startup goes, on the example of the Clients guide: Consumer needs only Kafka, so it does not wait for the slow Postgres, and the startup takes as long as its longest chain of dependencies.
With real connections the cost of a startup is the waiting, and the structure of the tree decides it. The backend of a product page: an API, four features, and every feature four connections of 100–300 ms, 21 clients:
nuke-di connects every client as soon as its own dependencies have, so the startup takes the longest chain, 0.34 s. dishka and wireup connect one client after another within a get(): 3.41 s, ten times as long, and the wider the tree, the larger the gap. Gathering the four features by hand brings wireup down to 0.95 s, and dishka too once its lock is off, which then builds a client that two features share twice. dependency-injector starts as fast once every client is a Resource written by hand, and stops layer by layer, every layer waiting for its slowest client: 0.66 s against 0.37 s, since Checkout and EventsProducer, which take 300 ms each to stop, are in different layers. injector has no async lifecycle.
$ uv run --group compare python benchmarks/compare.py --only connect --summary nuke-di 1.14.3 · CPython 3.11.7 · macOS-26.6.2-arm64-arm-64bit · commit 17c8815 · N = 10, 100, 1000 · 20 repeats nuke-di 1.14.3 · dishka 1.10.1 · wireup 2.12.1 · dependency-injector 4.49.1 · injector 0.24.0 | Lower is better | nuke-di | dishka | wireup | dependency-injector | injector | |------------------------------------------|------------:|---------------:|---------------:|--------------------:|---------:| | Startup: 8 clients, connect() of 1–60 ms | **70.8 ms** | 156 ms (2.2×) | 157 ms (2.2×) | **71.5 ms** | — | | Shutdown: the same 8 clients | **18.8 ms** | 30.9 ms (1.6×) | 30.5 ms (1.6×) | 26.0 ms (1.4×) | — | | Startup: the product page, 21 clients | **335 ms** | 3.41 s (10.2×) | 3.41 s (10.2×) | **337 ms** | — | | Shutdown: the product page | **365 ms** | 850 ms (2.3×) | 850 ms (2.3×) | 657 ms (1.8×) | — | None of the three connects anything when its container is created: unless the application gets the root at startup, its first request waits for the connections and fails with them. nuke-di connects every client in async with DI, and a client that cannot connect stops the startup.
benchmarks/compare.py runs the same trees of clients through dishka, wireup, dependency-injector and injector, each registering the same classes its own way: a cold container with the root resolved, on classes new to the process, the root again, and one FastAPI request through each library's integration:
So, is nuke-di the fastest? At building a tree with real type hints and at a FastAPI request, yes: dependency-injector and injector take 2–2.5 times as long for the tree, dishka and wireup 24–37 times as long, for validating the graph when the container is created, and wireup and dependency-injector twice as long per request. With string annotations dependency-injector, which reads no annotation, is ahead by a fifth. On a cached root nuke-di is level with wireup, and the Cython get() of dependency-injector wins by about 50 ns, a difference no application notices.
On its own, resolve() costs 3.5–6.3 µs per client, so a tree of 1000 clients is built in under 5.5 ms, and connect() adds 13–18 µs per client. docs/benchmarks.md explains every scenario, records the baseline on Python 3.11–3.14 and has the whole comparison with its method.
# report.py from nuke_di import Client, entrypoint class Reports(Client): async def publish(self) -> None: print("Report published") @entrypoint async def main(reports: Reports) -> None: await reports.publish() $ python -m report Report published The decorator resolves the function's clients, connects them, runs the function and disconnects the clients on exit. A normal import leaves the function available for a test or another caller. See Executable modules for migration and an explicit main() with application-owned arguments.
A path operation takes a client by its type hint, with no Depends and no inject() per handler. app/clients.py holds the Database and UserService classes of the Quick start, without its main():
pip install "nuke-di[fastapi]" # app/api.py from typing import Annotated from fastapi import Depends, FastAPI, Header from app.clients import Database, UserService from nuke_di.fastapi import setup app = FastAPI() setup(app) # before the routes: clients connect on startup, disconnect on shutdown @app.get("/users/{user_id}") async def get_user(user_id: int, users: UserService) -> str: return await users.greet(user_id) async def current_user(x_user_id: Annotated[int, Header()], db: Database) -> str: return await db.fetch_user(x_user_id) @app.get("/me") async def me(user: Annotated[str, Depends(current_user)]) -> str: return user $ uvicorn app.api:app INFO: Started server process [55625] INFO: Waiting for application startup. database: connected INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: 127.0.0.1:51602 - "GET /users/42 HTTP/1.1" 200 OK INFO: 127.0.0.1:51604 - "GET /me HTTP/1.1" 200 OK ^C INFO: Shutting down INFO: Waiting for application shutdown. database: disconnected INFO: Application shutdown complete. INFO: Finished server process [55625] $ curl localhost:8000/users/42 "Hello, user-42!" $ curl localhost:8000/me -H "X-User-Id: 7" "user-7" The clients connect on startup and disconnect on shutdown, and a dependency such as current_user takes clients the same way. Importing the app builds nothing, so a test replaces a client before TestClient starts it:
# tests/test_api.py from fastapi.testclient import TestClient from app.api import app from app.clients import Database from nuke_di import DI class FakeDatabase(Database): async def fetch_user(self, user_id: int) -> str: return "alice" def test_get_user() -> None: with DI.override(Database, FakeDatabase()), TestClient(app) as client: assert client.get("/users/1").json() == "Hello, alice!" assert client.get("/me", headers={"X-User-Id": "7"}).json() == "alice" $ pytest -q tests/test_api.py . [100%] 1 passed in 0.23s Routers, websockets and the app's own lifespan are covered in FastAPI; Litestar, FastStream, MCP servers, aiogram and taskiq work the same way.
Clients: Client, the lifecycle, dataclass clients, connect order, connect and resolution errors
The container: Dependencies and the global DI, resolve(), inject(), mock(), override()
Lifecycle guarantees: ordering, failures, cancellation and their regression tests
Executable modules: @entrypoint, explicit process control and migration from 2.x
Frameworks: FastAPI, Litestar, FastStream, MCP servers on the SDK and on FastMCP, aiogram, taskiq, Starlette, Quart and any ASGI app, and servers with an explicit lifespan
Testing: mock(), override(), the pytest fixtures, checking the wiring
Configuration: client timeouts and the disconnect deadline
Errors: every exception and when it is raised
Examples: runnable scenarios, from a one-off script and a queue worker to FastAPI, Litestar, FastStream, aiogram, taskiq, Starlette, an MCP server and a whole service, each with its output and tests
Benchmarks: every scenario, the baseline on Python 3.11–3.14 and the comparison with other libraries
Coding agents: the Agent Skill, a block for AGENTS.md, llms.txt, Context7
Development: the checks, coverage and releases
Both files in this release have provenance metadata.
MIT View SPDX License List
aiogram fastapi fastmcp faststream litestar mcp taskiq
aiogram asgi async asyncio dependency-injection dependency-injection-container di fastapi fastmcp faststream inversion-of-control ioc kubernetes lifecycle litestar mcp microservices model-context-protocol pytest quart starlette taskiq telegram-bot type-hints
Development Status 5 - Production/Stable
Intended Audience Developers
Operating System OS Independent
Programming Language Python :: 3
Topic Software Development :: Libraries :: Application Frameworks
Software Development :: Libraries :: Python Modules
Release files for nuke-di 3.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
Built distribution (wheel)
Total release size: 327.3 kB
Release files / nuke_di-3.0.2.tar.gz
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Release files / nuke_di-3.0.2-py3-none-any.whl
Security for nuke-di 3.0.2
Both files in this release have been uploaded with provenance metadata.
With provenance nuke_di-3.0.2.tar.gz Nov 25, 2026
With provenance nuke_di-3.0.2-py3-none-any.whl Nov 25, 2026
Attestation for nuke_di-3.0.2.tar.gz
Attestation for nuke_di-3.0.2-py3-none-any.whl
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.
Release history Release notifications | RSS feed
Nov 25, 2026 2 release files Full provenance
Nov 24, 2026 2 release files Full provenance
Oct 25, 2026 2 release files Full provenance
Oct 23, 2026 2 release files Full provenance
Oct 17, 2026 2 release files Full provenance
Oct 11, 2026 2 release files Full provenance
Oct 10, 2026 2 release files Full provenance
Oct 9, 2026 2 release files Full provenance
Oct 8, 2026 2 release files Full provenance
Oct 7, 2026 2 release files Full provenance
"PyPI", "Python Package Index", and the blocks logos are registered trademarks of the Python Software Foundation.
© 2026 Python Software Foundation




