Skip to content

Commit 8806405

Browse files
committed
Add docs on @containers.copy() decorator
1 parent 13aa5fa commit 8806405

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

docs/containers/copying.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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::

docs/containers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Containers module API docs - :py:mod:`dependency_injector.containers`.
2323
dynamic
2424
specialization
2525
overriding
26+
copying
2627
reset_singletons
2728
check_dependencies
2829
traversal

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ follows `Semantic versioning`_
99

1010
Development version
1111
-------------------
12+
- Add docs on ``@containers.copy()`` decorator.
1213
- Refactor ``@containers.copy()`` decorator.
1314
- Refactor async mode support in containers module.
1415

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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)

0 commit comments

Comments
 (0)