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.2.0.tar.gz (4.2 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.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tinyinject-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3849a77575c311d6335441a0e79388943b4bfd93c3906a61f5130bb031b4a5bb
MD5 1fd6754553f84c162f833a48d1b8e5ef
BLAKE2b-256 bee9666f9cc7eaec1fd5c181c08aa5119fb0f3d4902c08af3c112723d81d07a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinyinject-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a6d2fa60556906f307db2ac20c22e6d9b51cba25ed0359bffc404b7f882759a
MD5 e347022b17939f366e0360495f817c0d
BLAKE2b-256 c4596ee8d7036cb75b2a5b4949b45a77c6737303b88570733b2b25222c735c0e

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