From 65f77044635e0cf039ae47e2512e2af7f6dde52a Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Mon, 23 Mar 2015 16:27:48 +0200 Subject: [PATCH] Minor refactoring --- objects/providers.py | 31 +++++++++++++++++-------------- tests/test_providers.py | 4 ++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/objects/providers.py b/objects/providers.py index c2c683f1..46692110 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -1,6 +1,6 @@ """Providers module.""" -from inspect import isclass +from six import class_types from .utils import ensure_is_provider from .utils import is_kwarg_injection @@ -80,7 +80,7 @@ class NewInstance(Provider): def __init__(self, provides, *injections): """Initializer.""" - if not isclass(provides): + if not isinstance(provides, class_types): raise Error('NewInstance provider expects to get class, ' + 'got {0} instead'.format(str(provides))) self.provides = provides @@ -151,6 +151,9 @@ class ExternalDependency(Provider): def __init__(self, instance_of): """Initializer.""" + if not isinstance(instance_of, class_types): + raise Error('ExternalDependency provider expects to get class, ' + + 'got {0} instead'.format(str(instance_of))) self.instance_of = instance_of super(ExternalDependency, self).__init__() @@ -260,14 +263,6 @@ class Config(Provider): self.value = value super(Config, self).__init__() - def update_from(self, value): - """Update current value from another one.""" - self.value.update(value) - - def __getattr__(self, item): - """Return instance of deferred config.""" - return _ChildConfig(parents=(item,), root_config=self) - def __call__(self, paths=None): """Return provided instance.""" value = self.value @@ -280,6 +275,14 @@ class Config(Provider): '"{0}" is undefined'.format('.'.join(paths))) return value + def __getattr__(self, item): + """Return instance of deferred config.""" + return _ChildConfig(parents=(item,), root_config=self) + + def update_from(self, value): + """Update current value from another one.""" + self.value.update(value) + class _ChildConfig(Provider): @@ -297,11 +300,11 @@ class _ChildConfig(Provider): self.root_config = root_config super(_ChildConfig, self).__init__() + def __call__(self, *args, **kwargs): + """Return provided instance.""" + return self.root_config(self.parents) + def __getattr__(self, item): """Return instance of deferred config.""" return _ChildConfig(parents=self.parents + (item,), root_config=self.root_config) - - def __call__(self, *args, **kwargs): - """Return provided instance.""" - return self.root_config(self.parents) diff --git a/tests/test_providers.py b/tests/test_providers.py index 1973d8b2..b545b476 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -313,6 +313,10 @@ class ExternalDependencyTests(unittest.TestCase): """Set test cases environment up.""" self.provider = ExternalDependency(instance_of=list) + def test_init_with_not_class(self): + """Test creation with not a class.""" + self.assertRaises(Error, ExternalDependency, object()) + def test_is_provider(self): """Test `is_provider` check.""" self.assertTrue(is_provider(self.provider))