Dependency Injection framework with IOC-container and scopes
Project description
"Modern-DI"
Project | Badges |
---|---|
common | |
modern-di | |
modern-di-fastapi | |
modern-di-litestar |
Dependency injection framework for Python inspired by dependency-injector
and dishka
.
It is in early development state and gives you the following:
- DI framework with IOC-container and scopes.
- Async and sync resolving.
- Python 3.10-3.13 support.
- Full coverage by types annotations (mypy in strict mode).
- Overriding dependencies for tests.
- Package with zero dependencies.
Describe resources and classes:
import dataclasses
import logging
import typing
logger = logging.getLogger(__name__)
# singleton provider with finalization
def create_sync_resource() -> typing.Iterator[str]:
logger.debug("Resource initiated")
try:
yield "sync resource"
finally:
logger.debug("Resource destructed")
# same, but async
async def create_async_resource() -> typing.AsyncIterator[str]:
logger.debug("Async resource initiated")
try:
yield "async resource"
finally:
logger.debug("Async resource destructed")
@dataclasses.dataclass(kw_only=True, slots=True)
class DependentFactory:
sync_resource: str
async_resource: str
Describe dependencies graph (IoC-container)
from modern_di import BaseGraph, Scope, providers
class Dependencies(BaseGraph):
sync_resource = providers.Resource(Scope.APP, create_sync_resource)
async_resource = providers.Resource(Scope.APP, create_async_resource)
simple_factory = providers.Factory(Scope.REQUEST, SimpleFactory, dep1="text", dep2=123)
dependent_factory = providers.Factory(
Scope.REQUEST,
sync_resource=sync_resource,
async_resource=async_resource,
)
Create container and resolve dependencies in your code
from modern_di import Container, Scope
# init container of app scope in sync mode
with Container(scope=Scope.APP) as app_container:
# resolve sync resource
Dependencies.sync_resource.sync_resolve(app_container)
# init container of app scope in async mode
async with Container(scope=Scope.APP) as app_container:
# resolve async resource
await Dependencies.async_resource.async_resolve(app_container)
# resolve sync resource
instance1 = await Dependencies.sync_resource.async_resolve(app_container)
instance2 = Dependencies.sync_resource.sync_resolve(app_container)
assert instance1 is instance2
# create container of request scope
async with app_container.build_child_container(scope=Scope.REQUEST) as request_container:
# resolve factories of request scope
Dependencies.simple_factory.sync_resolve(request_container)
await Dependencies.dependent_factory.async_resolve(request_container)
# resources of app-scope also can be resolved here
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
modern_di-0.6.0.tar.gz
(6.2 kB
view details)
Built Distribution
modern_di-0.6.0-py3-none-any.whl
(12.0 kB
view details)
File details
Details for the file modern_di-0.6.0.tar.gz
.
File metadata
- Download URL: modern_di-0.6.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47e98cd7689c3b50dfe1c3145cf80a00929189443f58921ec38cf8c974c513d7 |
|
MD5 | a684af2d78c026dc97ec00628df73081 |
|
BLAKE2b-256 | aa383be4a3c39faaf285d112fd9d6c50a45171521898e4c256a1835898cbd059 |
File details
Details for the file modern_di-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: modern_di-0.6.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9990ebcace060d0c5684937c6c3ac42d8d2158a7d45c1e6f04d65056679dd664 |
|
MD5 | 64003abf242f9892f4cd0c3acb214712 |
|
BLAKE2b-256 | 969d592ad6122cd65cad233036e683902e0a783b6122b731f761073e838af330 |