diff --git a/dependency_injector/injections.py b/dependency_injector/injections.py index 84ba1f0a..ac52dc9c 100644 --- a/dependency_injector/injections.py +++ b/dependency_injector/injections.py @@ -1,5 +1,7 @@ """Dependency injector injections module.""" +import warnings + import six from dependency_injector.providers.base import ( @@ -41,6 +43,10 @@ def inject(*args, **kwargs): def __init__(self, arg1, arg2): 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. :type args: tuple[object] @@ -50,6 +56,11 @@ def inject(*args, **kwargs): :return: Class / callable decorator :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) kwarg_injections = _parse_keyword_injections(kwargs) diff --git a/tests/test_injections.py b/tests/test_injections.py index 03cffad0..302ea8de 100644 --- a/tests/test_injections.py +++ b/tests/test_injections.py @@ -1,5 +1,7 @@ """Dependency injector injections unittests.""" +import warnings + import unittest2 as unittest from dependency_injector import injections @@ -176,3 +178,25 @@ class InjectTests(unittest.TestCase): @injections.inject(arg1=123) class Test(object): """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))