Add deprecation warning when @inject is used

This commit is contained in:
Roman Mogilatov 2016-09-16 16:02:59 +03:00
parent d9f4aed43c
commit 81072832e4
2 changed files with 35 additions and 0 deletions

View File

@ -1,5 +1,7 @@
"""Dependency injector injections module.""" """Dependency injector injections module."""
import warnings
import six import six
from dependency_injector.providers.base import ( from dependency_injector.providers.base import (
@ -41,6 +43,10 @@ def inject(*args, **kwargs):
def __init__(self, arg1, arg2): def __init__(self, arg1, arg2):
pass pass
.. deprecated:: 2.2.0
Usage of :py:func:`inject` decorator can lead to bad design and could
be considered as anti-pattern.
:param args: Tuple of context positional arguments. :param args: Tuple of context positional arguments.
:type args: tuple[object] :type args: tuple[object]
@ -50,6 +56,11 @@ def inject(*args, **kwargs):
:return: Class / callable decorator :return: Class / callable decorator
:rtype: (callable) -> (type | callable) :rtype: (callable) -> (type | callable)
""" """
warnings.warn(message='Call to a deprecated decorator - @{0}.{1}'
.format(inject.__module__, inject.__name__),
category=DeprecationWarning,
stacklevel=2)
arg_injections = _parse_positional_injections(args) arg_injections = _parse_positional_injections(args)
kwarg_injections = _parse_keyword_injections(kwargs) kwarg_injections = _parse_keyword_injections(kwargs)

View File

@ -1,5 +1,7 @@
"""Dependency injector injections unittests.""" """Dependency injector injections unittests."""
import warnings
import unittest2 as unittest import unittest2 as unittest
from dependency_injector import injections from dependency_injector import injections
@ -176,3 +178,25 @@ class InjectTests(unittest.TestCase):
@injections.inject(arg1=123) @injections.inject(arg1=123)
class Test(object): class Test(object):
"""Test class.""" """Test class."""
class InjectDeprecationTests(unittest.TestCase):
"""Deprecation of `@inject()` tests."""
def test_deprecation_warning_on_usage(self):
"""Test that DeprecationWarning is produced when `@inject` is used."""
with warnings.catch_warnings(record=True) as caught_warnings:
warnings.simplefilter('always')
@injections.inject(1)
def _example(arg):
pass
warnings.simplefilter('default')
self.assertEquals(len(caught_warnings), 1)
self.assertEquals(caught_warnings[-1].category, DeprecationWarning)
self.assertIn('Call to a deprecated decorator',
str(caught_warnings[-1].message))
self.assertIn('@dependency_injector.injections.inject',
str(caught_warnings[-1].message))