Python >=3.6 dependency injection based on attrs. Very simple, yet powerful.
Inspired by inject-attrs.
depsworks only with typing annotations defined as presented below!
- First, replace your
@attr.s(...)decorators: @attr.s(auto_attribs=True, <otherargs/kwargs...>)classThing: cfg: Configsome_dep: SomeClass
- With:
importdeps @deps.s(<otherargs/kwargs...>) classThing: cfg: IConfigsome_dep: ISomeClass
Notice that
auto_attribs=Trueis 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:
importdepsclassIConfig(deps.Interface) # ...classISomeClass(deps.Interface) # ...
- Now prepare interfaces implementations:
frominterfacesimportIConfig, ISomeClassclassConfig(IConfig) # ...classSomeClass(ISomeClass) # ...
Finally, configure dependency injection bindings provided by Inject package, using
bind_to_constructordirective.
MIT
Daniel Kuruc