Add docs and example for `Factory.add_attributes()` method

This commit is contained in:
Roman Mogylatov 2021-03-03 16:06:53 -05:00
parent f1a3ad0b82
commit 2cab6c687a
3 changed files with 40 additions and 0 deletions

View File

@ -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.

View File

@ -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
---------------------------------------------

View File

@ -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)