File tree Expand file tree Collapse file tree 5 files changed +48
-2
lines changed Expand file tree Collapse file tree 5 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -3,12 +3,21 @@ Container copying
3
3
4
4
You can create declarative container copies using ``@containers.copy() `` decorator.
5
5
6
- .. literalinclude :: ../../examples/containers/declarative_copy_decorator .py
6
+ .. literalinclude :: ../../examples/containers/declarative_copy_decorator1 .py
7
7
:language: python
8
8
:lines: 3-
9
9
:emphasize-lines: 18-22
10
10
11
11
Decorator ``@containers.copy() `` copies providers from source container to destination container.
12
12
Destination container provider will replace source provider, if names match.
13
13
14
+ Decorator ``@containers.copy() `` helps you when you create derived declarative containers
15
+ from the base one. Base container often keeps default dependencies while derived containers define
16
+ overriding providers. Without ``@containers.copy() `` decorator, overridden providers are available
17
+ in the derived container, but base class dependencies continue to be bound to the base class providers.
18
+
19
+ .. literalinclude :: ../../examples/containers/declarative_copy_decorator2.py
20
+ :language: python
21
+ :lines: 11-
22
+
14
23
.. disqus ::
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ that were made in every particular version.
7
7
From version 0.7.6 *Dependency Injector * framework strictly
8
8
follows `Semantic versioning `_
9
9
10
+ 4.25.1
11
+ ------
12
+ - Amend docs and add another example for ``@containers.copy() `` decorator.
13
+
10
14
4.25.0
11
15
------
12
16
- Add ``application-multiple-containers-runtime-overriding `` example. This example demonstrates
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ """Declarative container provider copying with ``@copy()`` decorator."""
2
+
3
+ from dependency_injector import containers , providers
4
+
5
+
6
+ class Service :
7
+ def __init__ (self , dependency : str ):
8
+ self .dependency = dependency
9
+
10
+
11
+ class Base (containers .DeclarativeContainer ):
12
+ dependency = providers .Dependency (instance_of = str , default = 'Default value' )
13
+ service = providers .Factory (Service , dependency = dependency )
14
+
15
+
16
+ @containers .copy (Base )
17
+ class Derived1 (Base ):
18
+ dependency = providers .Dependency (instance_of = str , default = 'Derived 1' )
19
+
20
+
21
+ # @containers.copy(Base) # <-- No @copy decorator
22
+ class Derived2 (Base ):
23
+ dependency = providers .Dependency (instance_of = str , default = 'Derived 2' )
24
+
25
+
26
+ if __name__ == '__main__' :
27
+ container1 = Derived1 ()
28
+ service1 = container1 .service ()
29
+ print (service1 .dependency ) # Derived 1
30
+
31
+ container2 = Derived2 ()
32
+ service2 = container2 .service ()
33
+ print (service2 .dependency ) # Default value
Original file line number Diff line number Diff line change 1
1
"""Top-level package."""
2
2
3
- __version__ = '4.25.0 '
3
+ __version__ = '4.25.1 '
4
4
"""Version number.
5
5
6
6
:type: str
You can’t perform that action at this time.
0 commit comments