An open API service indexing awesome lists of open source software.

https://github.com/danie1k/python-package-deps

Python Package: Dependency injection based on attrs
https://github.com/danie1k/python-package-deps

attrs dataclasses dependency-injection python python-attrs

Last synced: about 1 year ago
JSON representation

Python Package: Dependency injection based on attrs

Awesome Lists containing this project

README

          

****
deps
****

Python >=3.6 dependency injection based on attrs. Very simple, yet powerful.

Inspired by `inject-attrs `_.

Usage
-----
``deps`` **works only with typing annotations defined as presented below!**

1. First, replace your ``@attr.s(...)`` decorators:
.. code-block:: python

@attr.s(auto_attribs=True, )
class Thing:
cfg: Config
some_dep: SomeClass

With:
.. code-block:: python

import deps

@deps.s()
class Thing:
cfg: IConfig
some_dep: ISomeClass

Notice that ``auto_attribs=True`` is no more needed, it will be forced by this package.

2. Next you have to prepare - somewhere in your codebase - an abstract interfaces classes for your attributes:
.. code-block:: python

import deps

class IConfig(deps.Interface)
# ...

class ISomeClass(deps.Interface)
# ...

3. Now prepare interfaces implementations:
.. code-block:: python

from interfaces import IConfig, ISomeClass

class Config(IConfig)
# ...

class SomeClass(ISomeClass)
# ...

4. Finally, configure dependency injection bindings provided by `Inject `_ package, using ``bind_to_constructor`` directive.

License
-------
MIT

Creator
-------
Daniel Kuruc