Skip to main content

Minimalistic Dependency Injection for Python

Project description

dimi - Minimalistic Dependency Injection for Python

CI Coverage Python version

“Dependency Injection” is a 25-dollar term for a 5-cent concept.

James Shore

Tired of complex abstractions? Tired of things like "to do dependency injection let's first dive into providers, scopes and binders"?

Just want dead simple syntax for injecting dependencies and mocking them inside tests?

Say no more!

Installation

pip install dimi

Getting Started

from dimi import Container
from typing import Annotated
from flask import Flask, jsonify

# DI container instance stores all the dependencies you place inside
di = Container()


# dependency is a function, async function or a class
# it can be added to the container via the following decorator:

@di.dependency
def get_weather_service_url():
    return 'https://awesome-weather-provider.com/api'


# Dependency may have sub-dependencies defined via typing.Annotated
# For dimi mostly the second part of Annotated matters, the first part is for type checkers

@di.dependency
class WeatherService:
    def __init__(self, url: Annotated[str, get_weather_service_url])
        self.url = url

    def get_weather(self, city): ...


app = Flask(__name__)

# Annotated[MyCls, ...] is the shortcut for Annotated[MyCls, MyCls]

@app.route("/api/weather/<city>")
@di.inject
def get_weather(city: str, weather_service: Annotated[WeatherService, ...]):
    return jsonify(weather_service.get_weather())

The DI container also supports dict-like way of retrieving the dependencies:

weather_service = di[WeatherService]
print(weather_service.get_weather('london'))

OK, but where is the profit?

Someone may ask, "Why not just do this instead?"

# the same API handle but w/o DI

@app.route("/api/weather/<city>")
def get_weather(city: str):
    return jsonify(
        WeatherService('https://awesome-weather-provider.com/api').get_weather(city)
    )

The simplest argument for DI is easier testing.

How would you test the non-DI API handle above? I guess something nasty with monkey patches.

But with DI in action everything becomes much simpler and more obvious:

from myapp import di  # app-wide instance of the Container
from myapp.services import WeatherService


def test_weather_api_handle(test_client):
    class MockWeatherService:
        def get_weather(self, city):
            return {'temperature': 30.5, 'wind_speed': 3.1, 'city': city}

    # override() preserves and restores DI container state after exit from context manager
    with di.override({WeatherService: MockWeatherService}):
        weather = test_client.get('/api/weather/london').json()
        assert weather == {'temperature': 30.5, 'wind_speed': 3.1, 'city': 'london'}

Key Benefits

  • Simple and concise API:
    • Ask for dependencies via Annotated[SomeType, some_callable] type annotation
    • Add callables to the DI container via @di.dependency
    • Do injections via @di.inject
    • override DI contents via di.override()
  • Explicit dependencies wiring. No magic, no complicated or hidden rules. You define each (sub-)dependency explicitly inside Annotated[]
  • Auto sub-dependencies resolving. A may depend on B which may depend on C which may depend on D and E and so on. All of this will be correctly tied together and resolved at the time of a function call.
  • Optional scopes. Just use @di.dependency(scope=Singleton) to cache first call of a function for the lifetime of the app.
  • Async support
  • Thread safety

Docs

Want to know more? Welcome to the docs

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

dimi-1.5.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

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

dimi-1.5.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file dimi-1.5.0.tar.gz.

File metadata

  • Download URL: dimi-1.5.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dimi-1.5.0.tar.gz
Algorithm Hash digest
SHA256 789f72d601b1dda0aa275439fbfecf3ef27da2251807a1f28202baddb2bf0d3c
MD5 1d253694f636ab83d27e1c486c4af4cd
BLAKE2b-256 c8ffbebe30d1c28d8b38d21ec64745799ab6ff2c90ab943be1258c3b7b13e74c

See more details on using hashes here.

File details

Details for the file dimi-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: dimi-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dimi-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa9f55bd6ef14e8eba5feab926bea593b33cef44835da80e72a4fcf8c561b2ec
MD5 9395dddde30162dd702ab8f8663decd5
BLAKE2b-256 8593259ca2d0821a4daf29bc34016707e745ac3bdf7721b9b73324a540b58984

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