File tree Expand file tree Collapse file tree 4 files changed +47
-0
lines changed Expand file tree Collapse file tree 4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ Container copying
2
+ -----------------
3
+
4
+ You can create declarative container copies using ``@containers.copy() `` decorator.
5
+
6
+ .. literalinclude :: ../../examples/containers/declarative_copy_decorator.py
7
+ :language: python
8
+ :lines: 3-
9
+ :emphasize-lines: 18-22
10
+
11
+ Decorator ``@containers.copy() `` copies providers from source container to destination container.
12
+ Destination container provider will replace source provider, if names match.
13
+
14
+ .. disqus ::
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ Containers module API docs - :py:mod:`dependency_injector.containers`.
23
23
dynamic
24
24
specialization
25
25
overriding
26
+ copying
26
27
reset_singletons
27
28
check_dependencies
28
29
traversal
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ follows `Semantic versioning`_
9
9
10
10
Development version
11
11
-------------------
12
+ - Add docs on ``@containers.copy() `` decorator.
12
13
- Refactor ``@containers.copy() `` decorator.
13
14
- Refactor async mode support in containers module.
14
15
Original file line number Diff line number Diff line change
1
+ """Declarative container provider copying with ``@copy()`` decorator."""
2
+
3
+ import sqlite3
4
+ from unittest import mock
5
+
6
+ from dependency_injector import containers , providers
7
+
8
+
9
+ class Service :
10
+ def __init__ (self , db ):
11
+ self .db = db
12
+
13
+
14
+ class SourceContainer (containers .DeclarativeContainer ):
15
+
16
+ database = providers .Singleton (sqlite3 .connect , ':memory:' )
17
+ service = providers .Factory (Service , db = database )
18
+
19
+
20
+ # Copy ``SourceContainer`` providers into ``DestinationContainer``:
21
+ @containers .copy (SourceContainer )
22
+ class DestinationContainer (SourceContainer ):
23
+
24
+ database = providers .Singleton (mock .Mock )
25
+
26
+
27
+ if __name__ == '__main__' :
28
+ container = DestinationContainer ()
29
+
30
+ service = container .service ()
31
+ assert isinstance (service .db , mock .Mock )
You can’t perform that action at this time.
0 commit comments