mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Removing of Factory restriction to operate with class types only, factory methods are valid now
This commit is contained in:
parent
040aad93f8
commit
2c2d4f7434
|
@ -101,8 +101,8 @@ class Factory(Provider):
|
||||||
|
|
||||||
def __init__(self, provides, *injections):
|
def __init__(self, provides, *injections):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
if not isinstance(provides, class_types):
|
if not callable(provides):
|
||||||
raise Error('Factory provider expects to get class, ' +
|
raise Error('Factory provider expects to get callable, ' +
|
||||||
'got {0} instead'.format(str(provides)))
|
'got {0} instead'.format(str(provides)))
|
||||||
self._provides = provides
|
self._provides = provides
|
||||||
self._kwargs = tuple((injection
|
self._kwargs = tuple((injection
|
||||||
|
@ -151,10 +151,10 @@ class Singleton(Provider):
|
||||||
|
|
||||||
__slots__ = ('_instance', '_factory')
|
__slots__ = ('_instance', '_factory')
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, provides, *injections):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self._instance = None
|
self._instance = None
|
||||||
self._factory = Factory(*args, **kwargs)
|
self._factory = Factory(provides, *injections)
|
||||||
super(Singleton, self).__init__()
|
super(Singleton, self).__init__()
|
||||||
|
|
||||||
def _provide(self, *args, **kwargs):
|
def _provide(self, *args, **kwargs):
|
||||||
|
|
|
@ -186,8 +186,12 @@ class FactoryTests(unittest.TestCase):
|
||||||
"""Test `is_provider` check."""
|
"""Test `is_provider` check."""
|
||||||
self.assertTrue(is_provider(Factory(self.Example)))
|
self.assertTrue(is_provider(Factory(self.Example)))
|
||||||
|
|
||||||
def test_init_with_not_class(self):
|
def test_init_with_callable(self):
|
||||||
"""Test creation of provider with not a class."""
|
"""Test creation of provider with a callable."""
|
||||||
|
self.assertTrue(Factory(credits))
|
||||||
|
|
||||||
|
def test_init_with_not_callable(self):
|
||||||
|
"""Test creation of provider with not a callable."""
|
||||||
self.assertRaises(Error, Factory, 123)
|
self.assertRaises(Error, Factory, 123)
|
||||||
|
|
||||||
def test_call(self):
|
def test_call(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user