Skip to main content

tinyinject

A minimalistic protocol driven dependency injection framework for python.

Installation

python -m pip install tinyinject

Examples

Registering implementation to the DI registry

# package/protocols.py
from typing import Protocol


class Arithmetic(Protocol):
    def sum(self, a: int, b: int) -> int:
        ...


# package/providers.py
from tinyinject import di
from protocols import Arithmetic


@di.implements(interface=Arithmetic)
class _ArithmeticImplementation:
    def sum(self, a: int, b: int) -> int:
        return a + b


# package/__init__.py
# IMPORTANT: this ensures provider gets registered in the DI
from . import providers  # noqa

Requesting dependencies via Require descriptor

# main.py
from tinyinject import di
from package.protocols import Arithmetic


class App:
    _dependency: Arthmetic = di.Require(Arithmetic)

    def format_sum(self, a: int, b: int):
        print(f"Sum of {a} and {b} is {self._dependency.sum(a, b)}")

Requesting dependencies via request_kwargs decorator

# main.py
from tinyinject import di
from package.protocols import Arithmetic


@di.require_kwargs(dependency=Arithmetic)
def format_sum(self, a: int, b: int, *, dependency: Arithmetic):
    print(f"Sum of {a} and {b} is {dependency.sum(a, b)}")

Dependency Injection of functions

# package/protocols.py
from typing import Protocol


class Sum(Protocol):
    def __call__(self, a: int, b: int) -> int:
        ...


# package/providers.py
from tinyinject import di
from .protocols import Sum


@di.implements(interface=Sum)
def sum_func(a: int, b: int) -> int:
    return a + b


# package/__init__.py
from . import providers  # noqa


# main.py
from tinyinject import di
from package.protocols import Sum


class App:
    _sum: Sum = di.Require(Sum)

    def format_sum(self, a: int, b: int) -> int:
        print(f"Sum of {a} and {b} is {self._sum(a, b)}")

Using override context manager

This is written with testing in mind, however it will work in any use case where temporary replacement of the provider is necessary.

# test_sum.py
from unittest import mock
from tinyinject import di
from src.package.protocols import Arithmetic
from src.main import App


class TestSum:
    def test_app_format_sum_always_call_sum_with_correct_parameters(self):
        spy = mock.MagicMock()

        with di.override(Arithmetic, using=spy):
            App().format_sum(1, 2)

        spy.sum.assert_called_with(1, 2)

Metadata

Release files for tinyinject 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinyinject 0.4.0
File Size Uploaded Provenance Actions
tinyinject-0.4.0.tar.gz 4.1 kB Without provenance Details for tinyinject-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tinyinject 0.4.0
File Interpreter ABI Platform Actions
tinyinject-0.4.0-py3-none-any.whl Python 3 none any Details for tinyinject-0.4.0-py3-none-any.whl

Total release size: 8.8 kB

Release files / tinyinject-0.4.0.tar.gz

Download URL tinyinject-0.4.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0a8cff94d03fc096df3895bd6537ab156594e30aba8334fd38813f85ec579d5f
BLAKE2b-256 checksum
How to use checksums
bd7abbdcbfe862ab7ed96e6eed11005f573bb599cee678ab7a722f1d4b0dbb96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / tinyinject-0.4.0-py3-none-any.whl

Download URL tinyinject-0.4.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6111d8b0f3a004a833a56a5c4f84d33882386bd5dc77e0e1a7793daa02cf1b6e
BLAKE2b-256 checksum
How to use checksums
96df0ccb9d2ccdb7a16e5721824c9ef86bc92b84bd1d43f555fd2cb49c863f13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Security for tinyinject 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page