Kink: Dependency Injection
Kink is a lightweight dependency injection (DI) library for Python, offering a straightforward way to manage and inject services and dependencies. It supports constructor, method, and property injection, leveraging Python's type hints for automatic resolution. The library is actively maintained, with new versions typically released every few months, ensuring compatibility with recent Python versions and addressing community feedback.
Common errors
-
TypeError: 'NoneType' object is not callable
cause A dependency required by an injected function or class constructor was not registered in the `kink.di` container, leading to `None` being injected for that dependency, which then cannot be called or accessed.fixRegister the missing dependency in the `kink.di` container before the injection occurs, e.g., `di[MyDependencyClass] = MyDependencyClass` or `di[str] = 'default value'`. -
TypeError: __init__() missing 1 required positional argument: 'some_arg'
cause Kink relies on type hints to identify and inject dependencies. This error often indicates that a constructor or function argument meant for injection either lacks a type hint or the type hint refers to a type not registered in the `di` container.fixAdd appropriate type hints to all arguments that Kink should resolve (e.g., `def __init__(self, my_dep: MyDependency):`) and ensure `MyDependency` is registered in `kink.di`. -
Exception: This version of Python is not supported.
cause You are attempting to run Kink version 0.8.0 or newer on Python 3.7, which is no longer supported.fixUpgrade your Python environment to version 3.8 or higher. For example, use pyenv to install a newer Python version.
Warnings
- breaking Kink dropped support for Python 3.7 in version 0.8.0. If you are using Python 3.7, you must either upgrade your Python version to 3.8+ or pin `kink` to a version older than 0.8.0 (e.g., `kink<0.8.0`).
- gotcha Prior to version 0.8.1, the `@inject` decorator might not have correctly resolved all arguments when used with keyword arguments (`kwargs`) in function signatures, leading to unexpected `TypeError`s.
- gotcha If using Kink with Pydantic objects and the caching mechanism, a bug existed in versions prior to 0.8.1 where injection with the cache wrapper could fail. This was fixed in 0.8.1.
- gotcha Early versions of Kink had limited or no official support for Python's `Protocol` types or `Optional` types in dependency resolution. While `Optional` support was improved in 0.7.0 and `Protocol` support in 0.8.0, older versions may behave inconsistently with these advanced type hints.
Install
-
pip install kink
Imports
- Container
from kink import Container
- inject
from kink import inject
- di
from kink import di
Quickstart
from kink import di, inject
class MyService:
def __init__(self, message_prefix: str):
self.message_prefix = message_prefix
def get_greeting(self, name: str) -> str:
return f"{self.message_prefix}, {name}!"
@inject
def run_application(service: MyService):
print(service.get_greeting("World"))
# Configure the Dependency Injection container
di[str] = "Hello"
di[MyService] = MyService # Kink automatically resolves 'message_prefix' from di[str]
# Run the application, Kink injects MyService
run_application()