Skip to main content

Dependency injection for Python

Project description

Build Status Pypi version Scrutinizer Code Quality codecov

NOTE: This project is in ALPHA state

jacked ~ python on roids

  • A light and easy to use dependency injection framework.
  • Inject objects, functions, classes or lists containing any of these.
  • Let jacked automatically discover your injectables in a package.
  • Loose coupling on the juice!
  • Excellent for making your code testable!


pip install jacked


Inject instances

To inject instances, mark a class with the injectable decorator:

from jacked import injectable

class Cat:
    def sound(self):
        return 'meow'

You can now inject it in a function anywhere. Place the inject decorator on top of the function or method. Let jacked know what type to inject by type hinting your parameters:

def what_sound_does_it_make(cat: Cat):


Inject functions

Injecting functions works similarly. Just make sure that your function has the proper type hints:

def some_func(x: int, y: int) -> str:
    return f'The sum of {x} and {y} is {x + y}'

And like with instances, inject as follows:

def do_something(func: Callable[[int, int], str]):
    print(func(21, 21))


Inject classes

Assuming that we have the same Cat injectable like before, we can inject that class as follows:

from typing import Type

def do_something(cat_type: Type[Cat]):


Inject lists

Let's suppose that we have the following two injectables of the same parent:

class Animal(ABC):
    def sound(self):
        raise NotImplementedError

class Cat(Animal):
    def sound(self):
        return 'meow'

class Dog(Animal):
    def sound(self):
        return 'bark'

You can now inject them in a list:

def what_sound_does_it_make(animals: List[Animal]):
    for animal in animals:
        print(f'The {animal.__class__.__name__} does {animal.sound()}')


You could have also injected a list of classes or functions by hinting List[Type[...]] or List[Callable[...]] (the ... replaced by your injection target).


You can annotate an injectable as singleton, meaning that if the injectable is a class, only one instance is ever injected:

class Dog(Animal):
    def sound(self):
        return 'bark'

Auto discovery

You can let jacked discover injectables in some package using the discover function:

from jacked import discover


All python modules in that package are imported and the injectables are registered.

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

jacked-1.0.0a3.tar.gz (11.6 kB view hashes)

Uploaded Source

Built Distribution

jacked-1.0.0a3-py3-none-any.whl (15.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page