{"id":9869,"library":"kink","title":"Kink: Dependency Injection","description":"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.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/kodemore/kink","tags":["dependency-injection","di","ioc","python","type-hints"],"install":[{"cmd":"pip install kink","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Container","correct":"from kink import Container"},{"symbol":"inject","correct":"from kink import inject"},{"note":"`di` is the global Container instance, commonly used for convenience.","symbol":"di","correct":"from kink import di"}],"quickstart":{"code":"from kink import di, inject\n\nclass MyService:\n    def __init__(self, message_prefix: str):\n        self.message_prefix = message_prefix\n\n    def get_greeting(self, name: str) -> str:\n        return f\"{self.message_prefix}, {name}!\"\n\n@inject\ndef run_application(service: MyService):\n    print(service.get_greeting(\"World\"))\n\n\n# Configure the Dependency Injection container\ndi[str] = \"Hello\"\ndi[MyService] = MyService # Kink automatically resolves 'message_prefix' from di[str]\n\n# Run the application, Kink injects MyService\nrun_application()","lang":"python","description":"This example demonstrates how to set up a service with a dependency (a string prefix) using Kink's global `di` container and the `@inject` decorator for automatic dependency resolution. The `MyService` class requires a `message_prefix` which Kink resolves from the `di[str]` registration. The `run_application` function then receives an instance of `MyService`."},"warnings":[{"fix":"Upgrade Python to 3.8 or higher. Alternatively, downgrade Kink: `pip install 'kink<0.8.0'`","message":"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`).","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"Upgrade Kink to version 0.8.1 or newer: `pip install --upgrade kink`.","message":"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.","severity":"gotcha","affected_versions":"<0.8.1"},{"fix":"Upgrade Kink to version 0.8.1 or newer: `pip install --upgrade kink`. This ensures correct interaction between Kink's injection and Pydantic objects with caching.","message":"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.","severity":"gotcha","affected_versions":"<0.8.1"},{"fix":"Ensure you are on Kink version 0.8.0 or newer to fully leverage `Protocol` and robust `Optional` type injection. Upgrade: `pip install --upgrade kink`.","message":"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.","severity":"gotcha","affected_versions":"<0.8.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Register the missing dependency in the `kink.di` container before the injection occurs, e.g., `di[MyDependencyClass] = MyDependencyClass` or `di[str] = 'default value'`.","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.","error":"TypeError: 'NoneType' object is not callable"},{"fix":"Add 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`.","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.","error":"TypeError: __init__() missing 1 required positional argument: 'some_arg'"},{"fix":"Upgrade your Python environment to version 3.8 or higher. For example, use pyenv to install a newer Python version.","cause":"You are attempting to run Kink version 0.8.0 or newer on Python 3.7, which is no longer supported.","error":"Exception: This version of Python is not supported."}]}