Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

35 Commits

Repository files navigation

python-di

CIcodecovpypidownloadsversionslicense

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:

fromtypingimportListclassRepo:
defread(self) ->List[str]:
raiseNotImplementedErrorclassDomainAction:
def__init__(self, repo: Repo):
self.repo=repodefpresent(self) ->str:
joined=", ".join(self.repo.read())
returnf"Data found: {joined}"

Application concretes located in mod_simple_impl.py:

fromtypingimportListfrommod_simpleimportRepoclassMockupRepo(Repo):
defread(self) ->List[str]:
return ["di", "test"]

Automatic application construction:

fromdi.declarativeimportDeclarativeApp, DeclarativeModule, scan_factoriesimportmod_simple, mod_simple_impldefmain():
# create app definitionapp_def=DeclarativeApp(
DeclarativeModule(
# automatically add factories from `mod_simple` and `mod_simple_impl`scan_factories(mod_simple, mod_simple_impl),
)
)
# build appinstance=app_def.build_instance()
# get initialized `DomainAction` objectaction, =instance.values_by_type(mod_simple.DomainAction)
# check app worksassertaction.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.

About

Fully automatic dependency injection for Python using annotations / type hints.

Topics

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages