Small extension for injections library.
Main idea for this kind of injections is to get rid from something like this:
# my_package/service.py
from very.black.magic.proxing import Local
proxy = Local()
service = proxy('service')
def init_service(config):
proxy.service = Service(config)
# my_package/handler.py
from .service import service
def handle_request():
service.do_something()
...and replace it with this:
# my_package/deps.py
import injections as di
@di.has
class CommonDeps:
service = di.depends(Service)
common_deps_provider = CommonDeps()
# at this point dependencies has not been injected
# my_package/handler.py
from .service import Service
from .deps import common_deps_provider
@inject_from(common_deps_provider)
def handle_request(service: Service):
service.do_something()
# and somewhere in main:
inj = inections.Container()
inj['service'] = Service(config)
inj.inject(common_deps_provider)