Easy dependency injection without wiring
Project description
Injection
Easy dependency injection for all, works with Python 3.8-3.12. Main features and advantages:
- support Python 3.8-3.12;
- works with FastAPI, Litestar, Flask and Django REST Framework;
- support dependency injection via
Annotated
inFastAPI
; - the code is fully typed and checked with mypy;
- no third-party dependencies;
- no wiring;
- the life cycle of objects (scope) is implemented by providers;
- overriding dependencies for testing;
- 100% code coverage;
- good documentation;
- intuitive and almost identical api with dependency-injector, which will allow you to easily migrate to injection (see migration from dependency injector);
Installation
pip install deps-injection
Compatibility between web frameworks and injection features
Framework | Dependency injection with @inject | Overriding providers | Dependency injection with @autoinject (experimental) |
---|---|---|---|
FastAPI | ✅ | ✅ | ➖ |
Flask | ✅ | ✅ | ✅ |
Django REST Framework | ✅ | ✅ | ✅ |
Litestar | ✅ | ✅ | ➖ |
Using example with FastAPI
from typing import Annotated
from unittest.mock import Mock
import pytest
from fastapi import APIRouter, Depends, FastAPI
from fastapi.testclient import TestClient
from injection import DeclarativeContainer, Provide, inject, providers
class Settings:
redis_url: str = "redis://localhost"
redis_port: int = 6379
class Redis:
def __init__(self, *, url: str, port: int):
self.uri = url + ":" + str(port)
self.url = url
self.port = port
def get(self, key):
return key
class Container(DeclarativeContainer):
settings = providers.Singleton(Settings)
redis = providers.Singleton(
Redis,
port=settings.provided.redis_port,
url=settings.provided.redis_url,
)
router = APIRouter(prefix="/api")
def create_app():
app = FastAPI()
app.include_router(router)
return app
RedisDependency = Annotated[Redis, Depends(Provide[Container.redis])]
@router.get("/values")
@inject
def some_get_endpoint_handler(redis: RedisDependency):
value = redis.get(299)
return {"detail": value}
Testing example with overriding providers for above FastAPI example
@pytest.fixture(scope="session")
def app():
return create_app()
@pytest.fixture(scope="session")
def container():
return Container.instance()
@pytest.fixture()
def test_client(app):
client = TestClient(app)
return client
def test_override_providers(test_client, container):
def mock_get_method(_):
return "mock_get_method"
mock_redis = Mock()
mock_redis.get = mock_get_method
providers_to_override = {"redis": mock_redis}
with container.override_providers(providers_to_override):
response = test_client.get("/api/values")
assert response.status_code == 200
body = response.json()
assert body["detail"] == "mock_get_method"
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
deps_injection-1.5.2.tar.gz
(11.1 kB
view details)
Built Distribution
File details
Details for the file deps_injection-1.5.2.tar.gz
.
File metadata
- Download URL: deps_injection-1.5.2.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dbf00712a7a3a4ff5fcf4ce87b84ba794fc63aa610b4384d762649ae0fae060 |
|
MD5 | a6ef69f8c3e4cc2f2a7fe3bd3926e8bd |
|
BLAKE2b-256 | f3cf8b59574d3cbb44fd7662ca306aba1dc5a108d2c85b9163d910bf70943090 |
Provenance
The following attestation bundles were made for deps_injection-1.5.2.tar.gz
:
Publisher:
publish.yml
on nightblure/injection
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
deps_injection-1.5.2.tar.gz
- Subject digest:
4dbf00712a7a3a4ff5fcf4ce87b84ba794fc63aa610b4384d762649ae0fae060
- Sigstore transparency entry: 149459356
- Sigstore integration time:
- Predicate type:
File details
Details for the file deps_injection-1.5.2-py3-none-any.whl
.
File metadata
- Download URL: deps_injection-1.5.2-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 572744a48f4c3681596305890b4b0e75a051ae597a4da6cb2e9238be600ed4fa |
|
MD5 | 38f80bb6cb9a4bfd91362fc866b029b6 |
|
BLAKE2b-256 | 524954c8570c2af9f90419785bbd8fc385446f4fce376c68cd8ea197f277cf7f |
Provenance
The following attestation bundles were made for deps_injection-1.5.2-py3-none-any.whl
:
Publisher:
publish.yml
on nightblure/injection
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
deps_injection-1.5.2-py3-none-any.whl
- Subject digest:
572744a48f4c3681596305890b4b0e75a051ae597a4da6cb2e9238be600ed4fa
- Sigstore transparency entry: 149459358
- Sigstore integration time:
- Predicate type: