Removing of Factory restriction to operate with class types only, factory methods are valid now

This commit is contained in:
Roman Mogilatov 2015-07-24 00:28:52 +03:00
parent 040aad93f8
commit 2c2d4f7434
2 changed files with 10 additions and 6 deletions

View File

@ -101,8 +101,8 @@ class Factory(Provider):
def __init__(self, provides, *injections):
"""Initializer."""
if not isinstance(provides, class_types):
raise Error('Factory provider expects to get class, ' +
if not callable(provides):
raise Error('Factory provider expects to get callable, ' +
'got {0} instead'.format(str(provides)))
self._provides = provides
self._kwargs = tuple((injection
@ -151,10 +151,10 @@ class Singleton(Provider):
__slots__ = ('_instance', '_factory')
def __init__(self, *args, **kwargs):
def __init__(self, provides, *injections):
"""Initializer."""
self._instance = None
self._factory = Factory(*args, **kwargs)
self._factory = Factory(provides, *injections)
super(Singleton, self).__init__()
def _provide(self, *args, **kwargs):

View File

@ -186,8 +186,12 @@ class FactoryTests(unittest.TestCase):
"""Test `is_provider` check."""
self.assertTrue(is_provider(Factory(self.Example)))
def test_init_with_not_class(self):
"""Test creation of provider with not a class."""
def test_init_with_callable(self):
"""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)
def test_call(self):