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 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)
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.4.0.tar.gz
(4.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinyinject-0.4.0.tar.gz.
File metadata
- Download URL: tinyinject-0.4.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a8cff94d03fc096df3895bd6537ab156594e30aba8334fd38813f85ec579d5f
|
|
| MD5 |
437691c6b65374a8d14567a2bba8a5d2
|
|
| BLAKE2b-256 |
bd7abbdcbfe862ab7ed96e6eed11005f573bb599cee678ab7a722f1d4b0dbb96
|
File details
Details for the file tinyinject-0.4.0-py3-none-any.whl.
File metadata
- Download URL: tinyinject-0.4.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6111d8b0f3a004a833a56a5c4f84d33882386bd5dc77e0e1a7793daa02cf1b6e
|
|
| MD5 |
6942dcb1b56f5626fdb03f8efb49c630
|
|
| BLAKE2b-256 |
96df0ccb9d2ccdb7a16e5721824c9ef86bc92b84bd1d43f555fd2cb49c863f13
|