mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Add functionality for using positional argument injections with @inject decorator
This commit is contained in:
parent
a37f1fc256
commit
6dc007c8df
|
@ -77,10 +77,10 @@ class Method(NamedInjection):
|
||||||
def inject(*args, **kwargs):
|
def inject(*args, **kwargs):
|
||||||
"""Dependency injection decorator.
|
"""Dependency injection decorator.
|
||||||
|
|
||||||
:type injection: Injection
|
|
||||||
:return: (callable) -> (callable)
|
:return: (callable) -> (callable)
|
||||||
"""
|
"""
|
||||||
injections = _parse_kwargs_injections(args, kwargs)
|
arg_injections = _parse_args_injections(args)
|
||||||
|
kwarg_injections = _parse_kwargs_injections(args, kwargs)
|
||||||
|
|
||||||
def decorator(callback_or_cls):
|
def decorator(callback_or_cls):
|
||||||
"""Dependency injection decorator."""
|
"""Dependency injection decorator."""
|
||||||
|
@ -99,17 +99,20 @@ def inject(*args, **kwargs):
|
||||||
|
|
||||||
callback = callback_or_cls
|
callback = callback_or_cls
|
||||||
if hasattr(callback, 'injections'):
|
if hasattr(callback, 'injections'):
|
||||||
callback.injections += injections
|
callback.args += arg_injections
|
||||||
|
callback.kwargs += kwarg_injections
|
||||||
|
callback.injections += arg_injections + kwarg_injections
|
||||||
return callback
|
return callback
|
||||||
|
|
||||||
@six.wraps(callback)
|
@six.wraps(callback)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
"""Decorated with dependency injection callback."""
|
"""Decorated with dependency injection callback."""
|
||||||
return callback(*args,
|
return callback(*_get_injectable_args(args, decorated.args),
|
||||||
**_get_injectable_kwargs(kwargs,
|
**_get_injectable_kwargs(kwargs, decorated.kwargs))
|
||||||
decorated.injections))
|
|
||||||
|
|
||||||
decorated.injections = injections
|
decorated.args = arg_injections
|
||||||
|
decorated.kwargs = kwarg_injections
|
||||||
|
decorated.injections = arg_injections + kwarg_injections
|
||||||
|
|
||||||
return decorated
|
return decorated
|
||||||
return decorator
|
return decorator
|
||||||
|
|
|
@ -75,8 +75,89 @@ class MethodTests(unittest.TestCase):
|
||||||
class InjectTests(unittest.TestCase):
|
class InjectTests(unittest.TestCase):
|
||||||
"""Inject decorator test cases."""
|
"""Inject decorator test cases."""
|
||||||
|
|
||||||
def test_decorated(self):
|
def test_decorated_args(self):
|
||||||
"""Test `inject()` decorated callback."""
|
"""Test `inject()` decoration with args."""
|
||||||
|
provider1 = di.Factory(object)
|
||||||
|
provider2 = di.Factory(list)
|
||||||
|
|
||||||
|
@di.inject(provider1, provider2)
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test()
|
||||||
|
a2, b2 = test()
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIsNot(a1, a2)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
||||||
|
def test_decorated_args_extended_syntax(self):
|
||||||
|
"""Test `inject()` decoration with args."""
|
||||||
|
provider1 = di.Factory(object)
|
||||||
|
provider2 = di.Factory(list)
|
||||||
|
|
||||||
|
@di.inject(di.Arg(provider1), di.Arg(provider2))
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test()
|
||||||
|
a2, b2 = test()
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIsNot(a1, a2)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
||||||
|
def test_decorated_args_several_times(self):
|
||||||
|
"""Test `inject()` decoration with args several times."""
|
||||||
|
provider1 = di.Factory(object)
|
||||||
|
provider2 = di.Factory(list)
|
||||||
|
|
||||||
|
@di.inject(provider2)
|
||||||
|
@di.inject(provider1)
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test()
|
||||||
|
a2, b2 = test()
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIsNot(a1, a2)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
||||||
|
def test_decorated_context_args(self):
|
||||||
|
"""Test `inject()` decoration with context args."""
|
||||||
|
provider1 = di.Factory(object)
|
||||||
|
provider2 = di.Factory(list)
|
||||||
|
|
||||||
|
@di.inject(provider1)
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test(provider2())
|
||||||
|
a2, b2 = test(provider2())
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIsNot(a1, a2)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
||||||
|
def test_decorated_kwargs(self):
|
||||||
|
"""Test `inject()` decoration with kwargs."""
|
||||||
provider1 = di.Factory(object)
|
provider1 = di.Factory(object)
|
||||||
provider2 = di.Factory(list)
|
provider2 = di.Factory(list)
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,7 @@ class Example(object):
|
||||||
|
|
||||||
def __init__(self, init_arg1=None, init_arg2=None, init_arg3=None,
|
def __init__(self, init_arg1=None, init_arg2=None, init_arg3=None,
|
||||||
init_arg4=None):
|
init_arg4=None):
|
||||||
"""Initializer.
|
"""Initializer."""
|
||||||
|
|
||||||
:param init_arg1:
|
|
||||||
:param init_arg2:
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
self.init_arg1 = init_arg1
|
self.init_arg1 = init_arg1
|
||||||
self.init_arg2 = init_arg2
|
self.init_arg2 = init_arg2
|
||||||
self.init_arg3 = init_arg3
|
self.init_arg3 = init_arg3
|
||||||
|
|
Loading…
Reference in New Issue
Block a user