Un deprecate container decorators (#310)
* Remove deprecation warnings * Add example and docs * Update changelog
This commit is contained in:
parent
97731db180
commit
5c1486e1a3
|
@ -21,4 +21,20 @@ The container also has:
|
|||
|
||||
:py:class:`DynamicContainer` has the same functionality.
|
||||
|
||||
Another possible way to override container providers on declarative level is
|
||||
``@containers.override()`` decorator:
|
||||
|
||||
.. literalinclude:: ../../examples/containers/declarative_override_decorator.py
|
||||
:language: python
|
||||
:lines: 3-
|
||||
:emphasize-lines: 12-16
|
||||
|
||||
Decorator ``@containers.override()`` takes a container for overriding as an argument.
|
||||
This container providers will be overridden by the providers with the same names from
|
||||
the decorated container.
|
||||
|
||||
It helps to change the behaviour of application by importing extension modules but not a code change.
|
||||
Imported module can override providers in main container. While the code uses main container as
|
||||
before, the overridden providers provide components defined in the extension module.
|
||||
|
||||
.. disqus::
|
||||
|
|
|
@ -9,6 +9,9 @@ follows `Semantic versioning`_
|
|||
|
||||
Develop
|
||||
-------
|
||||
- "Un-deprecate" ``@containers.override()`` and ``@containers.copy()`` decorators (
|
||||
see `Issue 301 <https://github.com/ets-labs/python-dependency-injector/issues/301>`_
|
||||
for more information).
|
||||
- Add favicon.
|
||||
- Remove redirects that occur while getting badge images to optimize docs load speed.
|
||||
- Update license year.
|
||||
|
@ -57,8 +60,6 @@ Deprecations:
|
|||
- Deprecate ``ext.aiohttp`` module in favor of ``wiring`` feature.
|
||||
- Deprecate ``ext.flask`` module in favor of ``wiring`` feature.
|
||||
- Deprecate ``.delegate()`` provider method in favor of ``.provider`` attribute.
|
||||
- Deprecate ``@containers.override()`` decorator in favor of overriding container on instance level.
|
||||
- Deprecate ``@containers.copy()`` decorator.
|
||||
|
||||
Removals:
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
"""Declarative container provider overriding with `@override()` decorator."""
|
||||
|
||||
import sqlite3
|
||||
from unittest import mock
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
database = providers.Singleton(sqlite3.connect, ':memory:')
|
||||
|
||||
|
||||
# Overriding `Container` with `OverridingContainer`:
|
||||
@containers.override(Container)
|
||||
class OverridingContainer(containers.DeclarativeContainer):
|
||||
|
||||
database = providers.Singleton(mock.Mock)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
container = Container()
|
||||
|
||||
database = container.database()
|
||||
assert isinstance(database, mock.Mock)
|
File diff suppressed because it is too large
Load Diff
|
@ -1,7 +1,6 @@
|
|||
"""Containers module."""
|
||||
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
import six
|
||||
|
||||
|
@ -428,11 +427,6 @@ def override(object container):
|
|||
:return: Declarative container's overriding decorator.
|
||||
:rtype: callable(:py:class:`DeclarativeContainer`)
|
||||
"""
|
||||
warnings.warn(
|
||||
'Decorator "@override()" is deprecated since version 4.0.3. '
|
||||
'Use overriding on instance level instead "container.override(AnotherContainer())".',
|
||||
category=DeprecationWarning,
|
||||
)
|
||||
def _decorator(object overriding_container):
|
||||
"""Overriding decorator."""
|
||||
container.override(overriding_container)
|
||||
|
@ -453,10 +447,6 @@ def copy(object container):
|
|||
:return: Declarative container's copying decorator.
|
||||
:rtype: callable(:py:class:`DeclarativeContainer`)
|
||||
"""
|
||||
warnings.warn(
|
||||
'Decorator "@copy()" is deprecated since version 4.0.3.',
|
||||
category=DeprecationWarning,
|
||||
)
|
||||
def _decorator(copied_container):
|
||||
cdef dict memo = dict()
|
||||
for name, provider in six.iteritems(copied_container.cls_providers):
|
||||
|
|
Loading…
Reference in New Issue