Skip to main content

Dependency injection for python.

Project description

Kink Build Status codecov

Dependency injection made for python

Features

  • Easy to use interface
  • Extensible with custom dependency resolvers
  • Automatic dependency injection
  • Lightweight
  • Easy to test

Installation

pip install kink

Usage

Simple dependency resolver

from kink import inject
from os import getenv

@inject(dsn=getenv("DB_DSN"), password=getenv("DB_PASSWORD"))
def get_database(dsn: str, password: str):
    ...

connection = get_database() # Will use `dsn` and `password` from env vars
connection_with_custom_dsn = get_database("my_dsn") # Only `password` will be taken from env vars
connection_with_custom_password = get_database(password="secret")

Nested dependencies resolving

from kink import inject
from os import getenv

@inject(dsn=getenv("DB_DSN"), password=getenv("DB_PASSWORD"))
def get_database_settings(dsn: str, password: str):
    ...

@inject(db_settings=get_database_settings)
def get_db_connection(db_settings: dict):
    ...

# This will create partially injected function
@inject(db_connection=get_db_connection)
def get_user(user_id: int, db_connection) -> dict:
    ...

get_user(12) # will use injected connection, connection will not be established until `get_user` function is called.

mock_connection = ...
get_user(12, mock_connection) # you can easily mock connections

Constructor injection

from kink import inject

def get_connection():
    ...

class UserRepository:
    @inject(db_connection=get_connection)
    def __init__(self, unit_of_work, db_connection):
        ...
    
    def get(self, id: int):
        ...

Setting dictionary as a resolver

from kink import inject, set_resolver

set_resolver({
    "gimme_a": "a",
    "gimme_b": "b",
})

@inject()
def print_a_b_c(gimme_a: str, gimme_b: str, gimme_c: str):
    print(gimme_a, gimme_b, gimme_c)


print_a_b_c(gimme_c="c") # will print; a, b, c

Defining custom dependency resolver

Kink supports two types of dependency resolvers:

  • callables which accepts 3 parameters; property name, property type and context
  • classes implementing kink.resolvers.Resolver protocol (see simple_resolver.py for example implementation)
from kink import inject, set_resolver
from kink.errors import ResolverError


def resolve_dependency_by_type(param_name: str, param_type: type, context):
    if param_type is str:
        return "test"

    if param_type is int:
        return 1

    raise ResolverError()

set_resolver(resolve_dependency_by_type)

@inject()
def test_me(one: int, test: str):
    print(one, test)

test_me() # will print: 1, "test"

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

kink-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kink-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file kink-0.1.1.tar.gz.

File metadata

  • Download URL: kink-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.5 Darwin/19.2.0

File hashes

Hashes for kink-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9627498c2fd35eb888e1ad0a92f2cb6bd653ae39c7780039ef0aa96b6637b249
MD5 de9e812720a70196109d06db1a56aff2
BLAKE2b-256 aedbc4de0efeaa5fa78f2e95c7782bdfd8ba1e0f7d620f3818ee4754a615d9ff

See more details on using hashes here.

File details

Details for the file kink-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: kink-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.5 Darwin/19.2.0

File hashes

Hashes for kink-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a59893d25175a8bb6144ce76b62eafb97c47e8406a63554544f3c1170b3a647d
MD5 c928959b33353be5247f6220a5da184e
BLAKE2b-256 85d19abe3fb383bda7d07a15e83d53f284f72ef1a751611cd8e3ddac5de1e2fb

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