Skip to main content

Fully automatic dependency injection for python

Project description

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.

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

python-di-1.1.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

python_di-1.1.0-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

Details for the file python-di-1.1.0.tar.gz.

File metadata

  • Download URL: python-di-1.1.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.8 Linux/5.4.0-1041-azure

File hashes

Hashes for python-di-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c3319d3b810390b96fc5a6147e08bfb423854cf99cdf8a93a8c5044b614249ed
MD5 d6956a7fd21e084e1535ecf987ac866c
BLAKE2b-256 b2104b74af76dbf64616cc28c7582fa6d324dc01d4936d3e9f3d6bb3d57e9f06

See more details on using hashes here.

File details

Details for the file python_di-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_di-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.8 Linux/5.4.0-1041-azure

File hashes

Hashes for python_di-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e08aec67f5343b1db36ea494066668d1d2649bee95f3f022bd9ba65280929829
MD5 4dee5e9ccd95fafe9907868854f0098d
BLAKE2b-256 9a0f1871a5db7c6ad6924b671e68ba6bccdb4dcbf46ca38d92d71d195d8a72f8

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