From bb33c925de94bc67a2cbef123924c7f3cc626de5 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Tue, 1 Sep 2015 15:21:55 +0300 Subject: [PATCH] Add simplified syntax of kwarg injections for ``Callable`` provider --- dependency_injector/providers.py | 11 +++++------ tests/test_providers.py | 8 ++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/dependency_injector/providers.py b/dependency_injector/providers.py index 92929ff4..b7ec57b6 100644 --- a/dependency_injector/providers.py +++ b/dependency_injector/providers.py @@ -255,22 +255,21 @@ class Callable(Provider): with some predefined dependency injections. """ - __slots__ = ('_callback', '_injections') + __slots__ = ('_callback', '_kwargs') - def __init__(self, callback, *injections): + def __init__(self, callback, **kwargs): """Initializer.""" if not callable(callback): raise Error('Callable expected, got {0}'.format(str(callback))) self._callback = callback - self._injections = tuple((injection - for injection in injections - if is_kwarg_injection(injection))) + self._kwargs = tuple((KwArg(name, value) + for name, value in six.iteritems(kwargs))) super(Callable, self).__init__() def _provide(self, *args, **kwargs): """Return provided instance.""" return self._callback(*args, **get_injectable_kwargs(kwargs, - self._injections)) + self._kwargs)) class Config(Provider): diff --git a/tests/test_providers.py b/tests/test_providers.py index e8e7de39..f221868b 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -429,9 +429,9 @@ class CallableTests(unittest.TestCase): def setUp(self): """Set test cases environment up.""" self.provider = di.Callable(self.example, - di.KwArg('arg1', 'a1'), - di.KwArg('arg2', 'a2'), - di.KwArg('arg3', 'a3')) + arg1='a1', + arg2='a2', + arg3='a3') def test_init_with_not_callable(self): """Test creation of provider with not callable.""" @@ -448,7 +448,7 @@ class CallableTests(unittest.TestCase): def test_call_with_args(self): """Test provider call with kwargs priority.""" provider = di.Callable(self.example, - di.KwArg('arg3', 'a3')) + arg3='a3') self.assertEqual(provider(1, 2), (1, 2, 'a3')) def test_call_with_kwargs_priority(self):