From 2a23f3d2f4b4f41509e57badc2b68a0291599aac Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 3 Sep 2020 17:03:28 -0400 Subject: [PATCH] Add container usage for list provider example --- docs/providers/list.rst | 2 +- examples/providers/list.py | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/providers/list.rst b/docs/providers/list.rst index afd24a3d..5f2d3924 100644 --- a/docs/providers/list.rst +++ b/docs/providers/list.rst @@ -13,7 +13,7 @@ List provider .. literalinclude:: ../../examples/providers/list.py :language: python :lines: 3- - :emphasize-lines: 19-22 + :emphasize-lines: 21-24 ``List`` provider handles positional arguments the same way as a :ref:`factory-provider`. diff --git a/examples/providers/list.py b/examples/providers/list.py index 1d2eb474..d7096e62 100644 --- a/examples/providers/list.py +++ b/examples/providers/list.py @@ -3,7 +3,7 @@ import dataclasses from typing import List -from dependency_injector import providers +from dependency_injector import containers, providers @dataclasses.dataclass @@ -16,23 +16,27 @@ class Dispatcher: modules: List[Module] -dispatcher_factory = providers.Factory( - Dispatcher, - modules=providers.List( - providers.Factory(Module, name='m1'), - providers.Factory(Module, name='m2'), - ), -) +class Container(containers.DeclarativeContainer): + + dispatcher_factory = providers.Factory( + Dispatcher, + modules=providers.List( + providers.Factory(Module, name='m1'), + providers.Factory(Module, name='m2'), + ), + ) if __name__ == '__main__': - dispatcher = dispatcher_factory() + container = Container() + + dispatcher = container.dispatcher_factory() assert isinstance(dispatcher.modules, list) assert dispatcher.modules[0].name == 'm1' assert dispatcher.modules[1].name == 'm2' - # Call "dispatcher = dispatcher_factory()" is an equivalent for: + # Call "dispatcher = container.dispatcher_factory()" is equivalent to: # dispatcher = Dispatcher( # modules=[ # Module(name='m1'),