Python >=3.6 dependency injection based on attrs. Very simple, yet powerful.
Inspired by inject-attrs.
deps
works only with typing annotations defined as presented below!
- First, replace your
@attr.s(...)
decorators: @attr.s(auto_attribs=True, <other args/kwargs...>) class Thing: cfg: Config some_dep: SomeClass
- With:
import deps @deps.s(<other args/kwargs...>) class Thing: cfg: IConfig some_dep: ISomeClass
Notice that
auto_attribs=True
is no more needed, it will be forced by this package.- First, replace your
- Next you have to prepare - somewhere in your codebase - an abstract interfaces classes for your attributes:
import deps class IConfig(deps.Interface) # ... class ISomeClass(deps.Interface) # ...
- Now prepare interfaces implementations:
from interfaces import IConfig, ISomeClass class Config(IConfig) # ... class SomeClass(ISomeClass) # ...
Finally, configure dependency injection bindings provided by Inject package, using
bind_to_constructor
directive.
MIT
Daniel Kuruc