A lightweight and extensible Dependency Injection (DI) framework for Python.
This project offers a minimalistic yet powerful approach to defining and resolving dependencies in Python applications using container-based injection and class decorators for automatic registration.
- 📦 Component Registration — Use
@component
to register classes into a DI container. - ⚙️ Method Injection — Use
@autowired
to inject dependencies into arbitrary methods or functions. - 🔄 Automatic Dependency Resolution — Constructor dependencies are automatically resolved and injected.
- 🧱 Custom Containers — Manage multiple containers for different contexts or scopes.
- 🔍 Type-safe Lookup — Retrieve components by type using indexing,
get_component
, orget_optional_component
. - 🔒 Immutable Containers — Containers become immutable after first use to ensure consistent state.
- ⏳ Async Support — Works seamlessly with
async def
functions and methods, including@autowired
injection.
Use the
di.aio
module for asyncio-compatible dependency injection in your project. The synchronous API is available indi
and is documented in di/basic_container/README.md.
For legacy usage examples without asyncio support, see: di/basic_container/README.md.
This is an example of how to register components using decorators and autowiring.
import asyncio
from di.aio import component, autowired
@component
class AsyncService:
async def fetch(self):
await asyncio.sleep(0.1)
return "Fetched async result"
class AsyncWorker:
@autowired
async def work(self, *, async_service: AsyncService):
result = await async_service.fetch()
print("Result:", result)
worker = AsyncWorker()
asyncio.run(worker.work())
The @autowired
decorator can be used outside of classes as well
import asyncio
from di.aio import component, autowired
@component
class AsyncService:
async def fetch(self):
await asyncio.sleep(0.1)
return "Fetched async result"
@autowired
async def work(self, *, async_service: AsyncService):
result = await async_service.fetch()
print("Result:", result)
asyncio.run(work())
This project is licensed under the Eclipse Public License 2.0. See the LICENSE file for more details.