python-dependency-injector/examples/wiring/example_attribute.py

30 lines
548 B
Python
Raw Normal View History

"""Wiring attribute example."""
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class Service:
...
class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
service: Service = Provide[Container.service]
class Main:
service: Service = Provide[Container.service]
2021-09-29 23:49:39 +03:00
if __name__ == "__main__":
container = Container()
2021-09-29 23:49:39 +03:00
container.wire(modules=[__name__])
assert isinstance(service, Service)
assert isinstance(Main.service, Service)