Skip to main content

Dependency injection for python.

Project description

Kink Build Status codecov

Dependency injection made for python


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


pip install kink


Adding service to dependency injection container

Dependency container is a dict-like object, adding new service to dependency container is as simple as the following example:

from kink import di
from os import getenv

di["db_name"] = getenv("DB_NAME")
di["db_password"] = getenv("DB_PASSWORD")

Adding service factory to dependency injection container

Kink also supports on-demand service creation. In order to define such a service, lambda function should be used:

from kink import di
from sqlite3 import connect

di["db_connection"] = lambda di: connect(di["db_name"])

In this scenario connection to database will not be established until service is requested.

Requesting services fromm dependency injection container

from kink import di
from sqlite3 import connect

# Setting services
di["db_name"] = "test_db.db"
di["db_connection"] = lambda di: connect(di["db_name"])

# Getting service

connection = di["db_connection"] # will return instance of sqlite3.Connection
assert connection == di["db_connection"] # True

Manual dependencies injection

from kink import di, inject
from sqlite3 import connect, Connection

di["db_name"] = "test_db.db"
di["db_connection"] = lambda di: connect(di["db_name"])

# Inject connection from di, connection is established once function is called.
def get_database(db_connection: Connection):

connection = get_database()
connection_with_passed_connection = get_database(connect("temp.db")) # will use passed connection

Constructor injection

from kink import inject, di
import MySQLdb

# Set dependencies
di["db_host"] = "localhost"
di["db_name"] = "test"
di["db_user"] = "user"
di["db_password"] = "password"
di["db_connection"] = lambda di: MySQLdb.connect(host=di["db_host"], user=di["db_user"], passwd=di["db_password"], db=di["db_name"])

class AbstractRepository:
    def __init__(self, db_connection):
        self.connection = db_connection

class UserRepository(AbstractRepository):

repository = UserRepository()
repository.connection # mysql db connection is resolved and available to use.

When class is annotated by inject annotation it will be automatically added to the container for future use, like autowiring.

For more examples check tests directory

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for kink, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size kink-0.2.0-py3-none-any.whl (5.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size kink-0.2.0.tar.gz (4.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page