mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-03-03 11:15:47 +03:00
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.
|
: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::
|
.. disqus::
|
||||||
|
|
|
@ -9,6 +9,9 @@ follows `Semantic versioning`_
|
||||||
|
|
||||||
Develop
|
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.
|
- Add favicon.
|
||||||
- Remove redirects that occur while getting badge images to optimize docs load speed.
|
- Remove redirects that occur while getting badge images to optimize docs load speed.
|
||||||
- Update license year.
|
- Update license year.
|
||||||
|
@ -57,8 +60,6 @@ Deprecations:
|
||||||
- Deprecate ``ext.aiohttp`` module in favor of ``wiring`` feature.
|
- Deprecate ``ext.aiohttp`` module in favor of ``wiring`` feature.
|
||||||
- Deprecate ``ext.flask`` 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 ``.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:
|
Removals:
|
||||||
|
|
||||||
|
|
25
examples/containers/declarative_override_decorator.py
Normal file
25
examples/containers/declarative_override_decorator.py
Normal file
|
@ -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."""
|
"""Containers module."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
@ -428,11 +427,6 @@ def override(object container):
|
||||||
:return: Declarative container's overriding decorator.
|
:return: Declarative container's overriding decorator.
|
||||||
:rtype: callable(:py:class:`DeclarativeContainer`)
|
: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):
|
def _decorator(object overriding_container):
|
||||||
"""Overriding decorator."""
|
"""Overriding decorator."""
|
||||||
container.override(overriding_container)
|
container.override(overriding_container)
|
||||||
|
@ -453,10 +447,6 @@ def copy(object container):
|
||||||
:return: Declarative container's copying decorator.
|
:return: Declarative container's copying decorator.
|
||||||
:rtype: callable(:py:class:`DeclarativeContainer`)
|
:rtype: callable(:py:class:`DeclarativeContainer`)
|
||||||
"""
|
"""
|
||||||
warnings.warn(
|
|
||||||
'Decorator "@copy()" is deprecated since version 4.0.3.',
|
|
||||||
category=DeprecationWarning,
|
|
||||||
)
|
|
||||||
def _decorator(copied_container):
|
def _decorator(copied_container):
|
||||||
cdef dict memo = dict()
|
cdef dict memo = dict()
|
||||||
for name, provider in six.iteritems(copied_container.cls_providers):
|
for name, provider in six.iteritems(copied_container.cls_providers):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user