Skip to main content

A minimalistic dependency injection framework

Project description

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 Require
from package.protocols import Arithmetic


class App:
    _dependency: Arthmetic = 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 require_kwargs
from package.protocols import Arithmetic


@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 Require
from package.protocols import Sum


class App:
    _sum: Sum = 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 override
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 override(Arithmetic, using=spy):
            App().format_sum(1, 2)
            
        spy.sum.assert_called_with(1, 2)

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

tinyinject-0.3.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinyinject-0.3.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file tinyinject-0.3.0.tar.gz.

File metadata

  • Download URL: tinyinject-0.3.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for tinyinject-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a484a77bc99168ca31006e29d41186231378173bce828eef9e11b92d1b08224e
MD5 08a62b3a36021a166b9b881a73d3f22a
BLAKE2b-256 4136ad0c5779262bede14f809c15116973602d5d45c8408ce5ae0abb6c2c8ca2

See more details on using hashes here.

File details

Details for the file tinyinject-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: tinyinject-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for tinyinject-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac961d42552caa6d98541751fde8b522238af3d8d6013f55cc2312390f7540d2
MD5 e42c8e900f4a4e6e9d866291efa42816
BLAKE2b-256 6fa68e92260ccc9451d4adace2edbc2824205fd999812b7d389eb7c4e1cd46fc

See more details on using hashes here.

Supported by

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