Skip to main content

python-di

CI codecov pypi downloads versions license

Fully automatic dependency injection for python 3.7, 3.8, 3.9, pypy3 using (not only) argument annotations / type hints.

Corresponds to clean architecture patterns and ideal for business applications created in DDD / Hexagonal architecture flavour. No external dependencies - uses only standard libraries.

Key features:

  • automatic type matching based on type hints / type annotations - no manual configuration is needed, it just works out of the box
  • configurable object aggregation injection - DI can join SomeClass objects and inject into argument annotated as Collection[SomeClass]
  • not harm existing codebase - no decorators, no extra metadata are needed in existing codebase to make app construction possible
  • no singletons or global DI process state - app or any app components can be instantiated independently as many times as needed
  • transparency of DI process - static dependency graph and injection plan is built, informative exceptions on error cases (like cyclic dependency or missing elements)

Help

Coming soon...

An Example

Application domain located in mod_simple.py:

from typing import List


class Repo:
    def read(self) -> List[str]:
        raise NotImplementedError


class DomainAction:
    def __init__(self, repo: Repo):
        self.repo = repo

    def present(self) -> str:
        joined = ", ".join(self.repo.read())
        return f"Data found: {joined}"

Application concretes located in mod_simple_impl.py:

from typing import List

from mod_simple import Repo


class MockupRepo(Repo):
    def read(self) -> List[str]:
        return ["di", "test"]

Automatic application construction:

from di.declarative import DeclarativeApp, DeclarativeModule, scan_factories
import mod_simple, mod_simple_impl


def main():
    # create app definition
    app_def = DeclarativeApp(
        DeclarativeModule(
            # automatically add factories from `mod_simple` and `mod_simple_impl`
            scan_factories(mod_simple, mod_simple_impl),
        )
    )

    # build app
    instance = app_def.build_instance()

    # get initialized `DomainAction` object
    action, = instance.values_by_type(mod_simple.DomainAction)

    # check app works
    assert action.present() == "Data found: di, test"

More examples

More working examples are available in tests/di/declarative/. Please see tests/di/declarative/test_build.py for reference.

Metadata

Release files for python-di 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-di 1.1.1
File Size Uploaded
python-di-1.1.1.tar.gz 23.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-di 1.1.1
File Interpreter ABI Platform
python_di-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 62.1 kB

Release files / python-di-1.1.1.tar.gz

Download URL python-di-1.1.1.tar.gz
Size 23.3 kB
Tags Source
SHA-256 checksum
How to use checksums
af23342513c555f73593682d196e03ec66d0176505da1827a1f9ee1318a32c14
BLAKE2b-256 checksum
How to use checksums
02bbdd64dce7bbe9dfbeae01ebca325178d6a372cc2634e6aa6eef8f6077c9bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.8.8 Linux/5.4.0-1043-azure

Release files / python_di-1.1.1-py3-none-any.whl

Download URL python_di-1.1.1-py3-none-any.whl
Size 38.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
421247bc116a476504c2a17e2800a899d0fc7a9f8cd9718552f0b17fbf951d03
BLAKE2b-256 checksum
How to use checksums
f5e042ed4e572f9d5450809817bb3e2085933c68a6b18b7cdec45915fe3ba459
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.8.8 Linux/5.4.0-1043-azure

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page