Skip to main content

Easy dependency injection without wiring

Project description

Injection

PyPI - Version GitHub License

PyPI - Python Version

Latest Release Documentation Status

Tests And Linting codecov

Ruff Hatch project pdm-managed MyPy Strict

Maintainability GitHub Downloads (all assets, all releases)

PyPI - Month Downloads GitHub Repo stars


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, Flask, Litestar and Django REST Framework;
  • support dependency injection via Annotated in FastAPI;
  • the code is fully typed and checked with mypy;
  • no third-party dependencies;
  • multiple containers;
  • overriding dependencies for tests without wiring;
  • 100% code coverage and very simple code;
  • 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

Using example

import sys

if sys.version_info >= (3, 9):
    from typing import Annotated
else:
    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}


@router.post("/values")
@inject
async def some_get_async_endpoint_handler(redis: RedisDependency):
    value = redis.get(399)
    return {"detail": value}


###################### TESTING ######################
@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


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.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

deps_injection-1.4.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file deps_injection-1.4.0.tar.gz.

File metadata

  • Download URL: deps_injection-1.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for deps_injection-1.4.0.tar.gz
Algorithm Hash digest
SHA256 9dc6b1c549f26615bb5aa36e353e54a6ffaaf8ba91bc6de6e1b115180c088137
MD5 b869ade83d398d2f321844c804d3fc4c
BLAKE2b-256 c483d764ae01a55f4ee9ff880ddf4bdd753af0b06fdeb6675c9549ca7ef86cfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for deps_injection-1.4.0.tar.gz:

Publisher: publish.yml on nightblure/injection

Attestations:

File details

Details for the file deps_injection-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for deps_injection-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbcdec40911d824ae1561d31a1266d8e84454fa4c2a01f36badd79791b7bf988
MD5 3dd108b5d2c1493b79adf86017a15760
BLAKE2b-256 ba2d7ae8e910940b4e6cdc61d5732dced44f08e8d95bd11dda234bcfbeb11f85

See more details on using hashes here.

Provenance

The following attestation bundles were made for deps_injection-1.4.0-py3-none-any.whl:

Publisher: publish.yml on nightblure/injection

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page