Skip to content

Commit 0026f48

Browse files
committed
Merge branch 'release/4.25.1' into master
2 parents 6a73b9d + e501734 commit 0026f48

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

docs/containers/copying.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ Container copying
33

44
You can create declarative container copies using ``@containers.copy()`` decorator.
55

6-
.. literalinclude:: ../../examples/containers/declarative_copy_decorator.py
6+
.. literalinclude:: ../../examples/containers/declarative_copy_decorator1.py
77
:language: python
88
:lines: 3-
99
:emphasize-lines: 18-22
1010

1111
Decorator ``@containers.copy()`` copies providers from source container to destination container.
1212
Destination container provider will replace source provider, if names match.
1313

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+
1423
.. disqus::

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+
4.25.1
11+
------
12+
- Amend docs and add another example for ``@containers.copy()`` decorator.
13+
1014
4.25.0
1115
------
1216
- Add ``application-multiple-containers-runtime-overriding`` example. This example demonstrates
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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

src/dependency_injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Top-level package."""
22

3-
__version__ = '4.25.0'
3+
__version__ = '4.25.1'
44
"""Version number.
55
66
:type: str

0 commit comments

Comments
 (0)