Skip to content

A little dependency injection framework for Python.

License

Notifications You must be signed in to change notification settings

duolingo/minject

Repository files navigation

minject

PyPI version Unit Tests Typing

Philosophy | Documentation | PyPI Homepage

Philosophy

minject is a dependency injection tool that aims to be easy to use, easy to understand, and easy to ignore. To accomplish this we follow these principles:

  • Minimize boilerplate: Adding dependency injection to a class should require as little extra code as possible. You can add or change a dependency with only one line of code.
  • Be obvious: We keep the dependency injection pieces close to the code, not separated into a different file. You should be able to understand where and how minject is working from looking at the source code.
  • Work as plain Python: Dependency injection must be easy to ignore. You can always use minject classes as if they were regular classes.

Here's an example to demonstrate:

import minject


@minject.define
class Engine:
    cylinders: int = minject.field(binding=4)


@minject.define
class Car:
    engine: Engine = minject.field(binding=minject.inject.reference(Engine))

This code follows the preceding principles in the following ways:

  • There isn't much boilerplate: one call to @minject.define to mark a class for injection, and one minject.field() to annotate what it depends on.
  • These annotations clearly show minject is involved and provide a starting point for learning more.
  • If you need to use this class without minject you can call my_car = Car(my_engine) and it will work how you'd expect without any hidden magic.

Documentation

PyPI

This library is available on PyPI (Homepage)


Duolingo logo
Duolingo is hiring! Apply at https://www.duolingo.com/careers

About

A little dependency injection framework for Python.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published