From d6e4e8fb089a18681c0580d04ef8f755ae6bb9e6 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 3 Sep 2020 17:48:45 -0400 Subject: [PATCH] Add container usage to provided instance examples --- docs/providers/provided_instance.rst | 4 +- examples/providers/provided_instance.py | 24 ++++++----- .../providers/provided_instance_complex.py | 40 ++++++++++--------- src/dependency_injector/providers.pyi | 4 +- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/docs/providers/provided_instance.rst b/docs/providers/provided_instance.rst index a53035cf..0bb7d664 100644 --- a/docs/providers/provided_instance.rst +++ b/docs/providers/provided_instance.rst @@ -14,7 +14,7 @@ You can inject provided object attribute, item or result of its method call. .. literalinclude:: ../../examples/providers/provided_instance.py :language: python - :emphasize-lines: 26-32 + :emphasize-lines: 28-34 :lines: 3- To use the feature you should use the ``.provided`` attribute of the injected provider. This @@ -32,7 +32,7 @@ You can do nested constructions: .. literalinclude:: ../../examples/providers/provided_instance_complex.py :language: python - :emphasize-lines: 24-30 + :emphasize-lines: 26-32 :lines: 3- The ``.provided`` attribute is available for the next providers: diff --git a/examples/providers/provided_instance.py b/examples/providers/provided_instance.py index e61f9bb7..bf63bc6f 100644 --- a/examples/providers/provided_instance.py +++ b/examples/providers/provided_instance.py @@ -1,6 +1,6 @@ """Example of the injecting of provided instance attributes and items.""" -from dependency_injector import providers +from dependency_injector import containers, providers class Service: @@ -23,17 +23,21 @@ class Client: self.value4 = value4 -service = providers.Singleton(Service) +class Container(containers.DeclarativeContainer): -client_factory = providers.Factory( - Client, - value1=service.provided[0], - value2=service.provided.value, - value3=service.provided.values[0], - value4=service.provided.get_value.call(), -) + service = providers.Singleton(Service) + + client_factory = providers.Factory( + Client, + value1=service.provided[0], + value2=service.provided.value, + value3=service.provided.values[0], + value4=service.provided.get_value.call(), + ) if __name__ == '__main__': - client = client_factory() + container = Container() + + client = container.client_factory() assert client.value1 == client.value2 == client.value3 == 'foo' diff --git a/examples/providers/provided_instance_complex.py b/examples/providers/provided_instance_complex.py index 20381f9b..108e1ebf 100644 --- a/examples/providers/provided_instance_complex.py +++ b/examples/providers/provided_instance_complex.py @@ -1,6 +1,6 @@ """Complex example of the injecting of provided instance attributes and items.""" -from dependency_injector import providers +from dependency_injector import containers, providers class Service: @@ -12,31 +12,35 @@ class Service: return self.value -service = providers.Singleton(Service, value=42) +class Container(containers.DeclarativeContainer): -dependency = providers.Object( - { - 'foo': { - 'bar': 10, - 'baz': lambda arg: {'arg': arg} + service = providers.Singleton(Service, value=42) + + dependency = providers.Object( + { + 'foo': { + 'bar': 10, + 'baz': lambda arg: {'arg': arg} + }, }, - }, -) + ) -demo_list = providers.List( - dependency.provided['foo']['bar'], - dependency.provided['foo']['baz'].call(22)['arg'], - dependency.provided['foo']['baz'].call(service)['arg'], - dependency.provided['foo']['baz'].call(service)['arg'].value, - dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(), -) + demo_list = providers.List( + dependency.provided['foo']['bar'], + dependency.provided['foo']['baz'].call(22)['arg'], + dependency.provided['foo']['baz'].call(service)['arg'], + dependency.provided['foo']['baz'].call(service)['arg'].value, + dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(), + ) if __name__ == '__main__': - assert demo_list() == [ + container = Container() + + assert container.demo_list() == [ 10, 22, - service(), + container.service(), 42, 42, ] diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index c8d97a70..53052c76 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -283,8 +283,8 @@ class Selector(Provider): class ProvidedInstanceFluentInterface: - def __getattr__(self, item: str) -> AttributeGetter: ... - def __getitem__(self, item: str) -> ItemGetter: ... + def __getattr__(self, item: Any) -> AttributeGetter: ... + def __getitem__(self, item: Any) -> ItemGetter: ... def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ...