mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add simplified syntax of kwarg injections for `Callable
` provider
This commit is contained in:
parent
a1cf735391
commit
bb33c925de
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user