Skip to content

Commit 2cab6c6

Browse files
committed
Add docs and example for Factory.add_attributes() method
1 parent f1a3ad0 commit 2cab6c6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

docs/main/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
Development version
11+
-------------------
12+
- Add docs and example for ``Factory.add_attributes()`` method.
13+
1014
4.29.0
1115
------
1216
- Implement context manager interface for resetting a singleton provider.

docs/providers/factory.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ injected following these rules:
4040
:language: python
4141
:lines: 3-
4242

43+
``Factory`` provider can inject attributes. Use ``.add_attributes()`` method to specify
44+
attribute injections.
45+
46+
.. literalinclude:: ../../examples/providers/factory_attribute_injections.py
47+
:language: python
48+
:lines: 3-
49+
:emphasize-lines: 18-18
50+
4351
Passing arguments to the underlying providers
4452
---------------------------------------------
4553

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""`Factory` provider attribute injections example."""
2+
3+
from dependency_injector import containers, providers
4+
5+
6+
class Client:
7+
...
8+
9+
10+
class Service:
11+
def __init__(self) -> None:
12+
self.client = None
13+
14+
15+
class Container(containers.DeclarativeContainer):
16+
17+
client = providers.Factory(Client)
18+
19+
service = providers.Factory(Service)
20+
service.add_attributes(clent=client)
21+
22+
23+
if __name__ == '__main__':
24+
container = Container()
25+
26+
service = container.service()
27+
28+
assert isinstance(service.client, Client)

0 commit comments

Comments
 (0)