Skip to main content

Dependency injection toolkit

Project description

di: dependency injection toolkit

Test Coverage Package version Supported Python versions

di is a modern dependency injection toolkit, modeled around the simplicity of FastAPI's dependency injection.

Key features:

  • Intuitive: simple API, inspired by FastAPI.
  • Auto-wiring: di supports auto-wiring using type annotations.
  • Scopes: inspired by pytest scopes, but defined by users (no fixed "request" or "session" scopes).
  • Composable: decoupled internal APIs give you the flexibility to customize wiring, execution and binding.
  • Performant: di can execute dependencies in parallel and cache results ins scopes. Performance critical parts are written in 🦀 via graphlib2.

Installation

pip install di[anyio]

⚠️ This project is a work in progress. Until there is 1.X.Y release, expect breaking changes. ⚠️

Simple Example

Here is a simple example of how di works:

from dataclasses import dataclass

from di import Container
from di.dependent import Dependent
from di.executors import SyncExecutor


class A:
    ...


class B:
    ...


@dataclass
class C:
    a: A
    b: B


def main():
    container = Container()
    executor = SyncExecutor()
    solved = container.solve(Dependent(C, scope="request"), scopes=["request"])
    with container.enter_scope("request") as state:
        c = solved.execute_sync(executor=executor, state=state)
    assert isinstance(c, C)
    assert isinstance(c.a, A)
    assert isinstance(c.b, B)

For more examples, see our docs.

Why do I need dependency injection in Python? Isn't that a Java thing?

Dependency injection is a software architecture technique that helps us achieve inversion of control and dependency inversion (one of the five SOLID design principles).

It is a common misconception that traditional software design principles do not apply to Python. As a matter of fact, you are probably using a lot of these techniques already!

For example, the transport argument to httpx's Client (docs) is an excellent example of dependency injection. Pytest, arguably the most popular Python test framework, uses dependency injection in the form of pytest fixtures.

Most web frameworks employ inversion of control: when you define a view / controller, the web framework calls you! The same thing applies to CLIs (like click) or TUIs (like Textual). This is especially true for many newer web frameworks that not only use inversion of control but also dependency injection. Two great examples of this are FastAPI and BlackSheep.

For a more comprehensive overview of Python projects related to dependency injection, see Awesome Dependency Injection in Python.

Project Aims

This project aims to be a dependency injection toolkit, with a focus on providing the underlying dependency injection functionality for other libraries.

In other words, while you could use this as a standalone dependency injection framework, you may find it to be a bit terse and verbose. There are also much more mature standalone dependency injection frameworks; I would recommend at least looking into python-dependency-injector since it is currently the most popular / widely used of the bunch.

For more background, see our docs.

See this release on GitHub: v0.79.2

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

di-0.79.2.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

di-0.79.2-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file di-0.79.2.tar.gz.

File metadata

  • Download URL: di-0.79.2.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

File hashes

Hashes for di-0.79.2.tar.gz
Algorithm Hash digest
SHA256 0c65b9ccb984252dadbdcdb39743eeddef0c1f167f791c59fcd70e97bb0d3af8
MD5 58ac022edfb1fd2d1425f27ba618faa7
BLAKE2b-256 bfd3e5ead09a065ac394bf4b16fa4e654db757db25743b83b281c7791497b7c9

See more details on using hashes here.

File details

Details for the file di-0.79.2-py3-none-any.whl.

File metadata

  • Download URL: di-0.79.2-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

File hashes

Hashes for di-0.79.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b2ac7c46d4d9e941ca47d37c2029ba739c1f8a0e19e5288731224870f00d6e6
MD5 6d280e7f64f807c3b35f9e0f36cdea48
BLAKE2b-256 ef529ab18ba80b87e998c19b4087fc65a5606e8b1adf883062903537c30868c3

See more details on using hashes here.

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