Add docs on @containers.copy() decorator

This commit is contained in:
Roman Mogylatov 2021-02-18 08:38:35 -05:00
parent 13aa5fa53d
commit 8806405f0f
4 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,14 @@
Container copying
-----------------
You can create declarative container copies using ``@containers.copy()`` decorator.
.. literalinclude:: ../../examples/containers/declarative_copy_decorator.py
:language: python
:lines: 3-
:emphasize-lines: 18-22
Decorator ``@containers.copy()`` copies providers from source container to destination container.
Destination container provider will replace source provider, if names match.
.. disqus::

View File

@ -23,6 +23,7 @@ Containers module API docs - :py:mod:`dependency_injector.containers`.
dynamic dynamic
specialization specialization
overriding overriding
copying
reset_singletons reset_singletons
check_dependencies check_dependencies
traversal traversal

View File

@ -9,6 +9,7 @@ follows `Semantic versioning`_
Development version Development version
------------------- -------------------
- Add docs on ``@containers.copy()`` decorator.
- Refactor ``@containers.copy()`` decorator. - Refactor ``@containers.copy()`` decorator.
- Refactor async mode support in containers module. - Refactor async mode support in containers module.

View File

@ -0,0 +1,31 @@
"""Declarative container provider copying with ``@copy()`` decorator."""
import sqlite3
from unittest import mock
from dependency_injector import containers, providers
class Service:
def __init__(self, db):
self.db = db
class SourceContainer(containers.DeclarativeContainer):
database = providers.Singleton(sqlite3.connect, ':memory:')
service = providers.Factory(Service, db=database)
# Copy ``SourceContainer`` providers into ``DestinationContainer``:
@containers.copy(SourceContainer)
class DestinationContainer(SourceContainer):
database = providers.Singleton(mock.Mock)
if __name__ == '__main__':
container = DestinationContainer()
service = container.service()
assert isinstance(service.db, mock.Mock)