From 2c2d4f743482f007f1e757d54160230a6da145a5 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 24 Jul 2015 00:28:52 +0300 Subject: [PATCH] Removing of Factory restriction to operate with class types only, factory methods are valid now --- objects/providers.py | 8 ++++---- tests/test_providers.py | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/objects/providers.py b/objects/providers.py index 8ef5a46f..7857cd4c 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -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): diff --git a/tests/test_providers.py b/tests/test_providers.py index 77843d33..16c9bf16 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -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):