diwire¶
Type-driven dependency injection for Python. Zero dependencies. Zero boilerplate.
diwire is a dependency injection container for Python 3.10+ that builds your object graph from type hints alone. It supports scoped lifetimes, async-first resolution, generator-based cleanup, and open generics.
Installation¶
uv add diwire
pip install diwire
Quick start¶
Define your classes. Resolve the top-level one. diwire figures out the rest.
from dataclasses import dataclass
from diwire import Container, Lifetime
@dataclass
class Database:
host: str = "localhost"
@dataclass
class UserRepository:
db: Database
@dataclass
class UserService:
repo: UserRepository
container = Container(autoregister_default_lifetime=Lifetime.TRANSIENT)
service = container.resolve(UserService)
print(service.repo.db.host) # => localhost
What to read next¶
Tutorial (runnable examples) - a step-by-step tutorial you can run and copy-paste
Core concepts - the concepts behind the tutorial (the “why it works”)
How-to guides - a cookbook of real-world scenarios (frameworks, patterns, testing)
API reference - API reference for the public surface area