
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
A minimalistic protocol driven dependency injection framework for python.
python -m pip install tinyinject
# 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
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)}")
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)}")
# 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)}")
override context managerThis 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)
FAQs
A minimalistic dependency injection framework
We found that tinyinject demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.