Skip to main content

Autowiring dependency injection

Project description

di: pythonic dependency injection

Test Coverage Package version Supported Python versions

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

Key features:

  • Intuitive: simple API, inspired by FastAPI.
  • Succinct: declare what you want, and di figures out how to assemble it using type annotations.
  • Scopes: inspired by pytest scopes, but defined by users (no fixed "request" or "session" scopes).
  • Customizable: decoupled internal APIs give you the flexibility to customize wiring and execution.
  • Performant: di can execute dependencies in parallel, move sync dependencies to threads and cache results. Performance critical parts are written in 🦀 via graphlib2.

Installation

pip install di

⚠️ 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, Dependant, SyncExecutor


class A:
    ...


class B:
    ...


@dataclass
class C:
    a: A
    b: B


def main():
    container = Container(scopes=["request"])
    solved = container.solve(Dependant(C, scope="request"))
    with container.enter_scope("request"):
        c = container.execute_sync(solved, executor=SyncExecutor())
    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 general dependency injection system, 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.61.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.61.2.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

di-0.61.2-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: di-0.61.2.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for di-0.61.2.tar.gz
Algorithm Hash digest
SHA256 c525ecb770cb5fc36d24f51e65b4dced9b4ef08f5757401df3e1ce8888a4470f
MD5 354a07b5e729e1ecf5c6fe38a828ec66
BLAKE2b-256 072839497770f881d05e74a60bf711f2d82f4220fc3c804ed5108d3b375bc88d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: di-0.61.2-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for di-0.61.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4fffb1d0eaaa96312cacef1ebebd8cb1c8d4daa4fa2f4fdf6e084123672b22a0
MD5 f25736afe58d72a515e77924afd9e35a
BLAKE2b-256 2560277c4844f862329a66e1a0942b2725276956bee242a280afe0888f5b7009

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