From 2cab6c687abc70358cbc799e6e18732f3a10d53e Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Wed, 3 Mar 2021 16:06:53 -0500 Subject: [PATCH] Add docs and example for ``Factory.add_attributes()`` method --- docs/main/changelog.rst | 4 +++ docs/providers/factory.rst | 8 ++++++ .../providers/factory_attribute_injections.py | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 examples/providers/factory_attribute_injections.py diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 51c0ba6e..d5dfc3e7 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,10 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +Development version +------------------- +- Add docs and example for ``Factory.add_attributes()`` method. + 4.29.0 ------ - Implement context manager interface for resetting a singleton provider. diff --git a/docs/providers/factory.rst b/docs/providers/factory.rst index a4e2aa29..7b83f21f 100644 --- a/docs/providers/factory.rst +++ b/docs/providers/factory.rst @@ -40,6 +40,14 @@ injected following these rules: :language: python :lines: 3- +``Factory`` provider can inject attributes. Use ``.add_attributes()`` method to specify +attribute injections. + +.. literalinclude:: ../../examples/providers/factory_attribute_injections.py + :language: python + :lines: 3- + :emphasize-lines: 18-18 + Passing arguments to the underlying providers --------------------------------------------- diff --git a/examples/providers/factory_attribute_injections.py b/examples/providers/factory_attribute_injections.py new file mode 100644 index 00000000..e9eaf528 --- /dev/null +++ b/examples/providers/factory_attribute_injections.py @@ -0,0 +1,28 @@ +"""`Factory` provider attribute injections example.""" + +from dependency_injector import containers, providers + + +class Client: + ... + + +class Service: + def __init__(self) -> None: + self.client = None + + +class Container(containers.DeclarativeContainer): + + client = providers.Factory(Client) + + service = providers.Factory(Service) + service.add_attributes(clent=client) + + +if __name__ == '__main__': + container = Container() + + service = container.service() + + assert isinstance(service.client, Client)