From 548847f83a6cc7e8e16a004722035ba8bde4e6f5 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Tue, 10 Nov 2015 19:47:45 +0200 Subject: [PATCH] Remove required DynamicCatalog.name attribute --- dependency_injector/__init__.py | 2 +- dependency_injector/catalogs.py | 17 ++++++++--------- tests/test_catalogs.py | 3 +-- tests/test_utils.py | 8 +++----- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/dependency_injector/__init__.py b/dependency_injector/__init__.py index 252c6cd3..f659392d 100644 --- a/dependency_injector/__init__.py +++ b/dependency_injector/__init__.py @@ -42,7 +42,7 @@ from .utils import ensure_is_catalog_bundle from .errors import Error -# Backward compatimility fix for versions < 0.11.* +# Backward compatibility for versions < 0.11.* from . import catalogs catalog = catalogs diff --git a/dependency_injector/catalogs.py b/dependency_injector/catalogs.py index 2a688760..fed5421c 100644 --- a/dependency_injector/catalogs.py +++ b/dependency_injector/catalogs.py @@ -35,8 +35,7 @@ class CatalogBundle(object): :rtype: CatalogBundle :return: Subclass of CatalogBundle """ - return type('{0}Bundle'.format(catalog.name), (cls,), - dict(catalog=catalog)) + return type('BundleSubclass', (cls,), dict(catalog=catalog)) def get(self, name): """Return provider with specified name or raise an error.""" @@ -69,19 +68,17 @@ class DynamicCatalog(object): """Catalog of providers.""" __IS_CATALOG__ = True - __slots__ = ('name', 'Bundle', 'providers', 'provider_names', - 'overridden_by') + __slots__ = ('Bundle', 'providers', 'provider_names', + 'overridden_by', 'name') - def __init__(self, name, **providers): + def __init__(self, **providers): """Initializer. :param name: Catalog's name :type name: str - :param kwargs: Dict of providers with their catalog names :type kwargs: dict[str, dependency_injector.providers.Provider] """ - self.name = name self.Bundle = CatalogBundle.sub_cls_factory(self) self.providers = dict() self.provider_names = dict() @@ -94,6 +91,8 @@ class DynamicCatalog(object): self.provider_names[provider] = name self.providers[name] = provider self.overridden_by = tuple() + self.name = '.'.join((self.__class__.__module__, + self.__class__.__name__)) def is_bundle_owner(self, bundle): """Check if catalog is bundle owner.""" @@ -191,9 +190,9 @@ class DeclarativeCatalogMetaClass(type): providers = cls_providers + inherited_providers cls.name = '.'.join((cls.__module__, cls.__name__)) - cls.catalog = DynamicCatalog(cls.name, **dict(providers)) + cls.catalog = DynamicCatalog(**dict(providers)) + cls.catalog.name = cls.name cls.Bundle = cls.catalog.Bundle - cls.cls_providers = dict(cls_providers) cls.inherited_providers = dict(inherited_providers) diff --git a/tests/test_catalogs.py b/tests/test_catalogs.py index fb295732..fed33ab8 100644 --- a/tests/test_catalogs.py +++ b/tests/test_catalogs.py @@ -273,8 +273,7 @@ class OverrideTests(unittest.TestCase): def test_overriding_with_dynamic_catalog(self): """Test catalog overriding with another dynamic catalog.""" - self.Catalog.override(di.DynamicCatalog('OverridingCatalog', - obj=di.Value(1), + self.Catalog.override(di.DynamicCatalog(obj=di.Value(1), another_obj=di.Value(2))) self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(self.Catalog.another_obj(), 2) diff --git a/tests/test_utils.py b/tests/test_utils.py index f77298e4..93a289a7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -217,7 +217,7 @@ class IsCatalogTests(unittest.TestCase): def test_with_dynamic_catalog(self): """Test with class.""" - self.assertTrue(di.is_catalog(di.DynamicCatalog('TestCatalog'))) + self.assertTrue(di.is_catalog(di.DynamicCatalog())) def test_with_child_class(self): """Test with parent class.""" @@ -244,8 +244,7 @@ class IsDynamicCatalogTests(unittest.TestCase): def test_with_dynamic_catalog(self): """Test with dynamic catalog.""" - self.assertTrue(di.is_dynamic_catalog(di.DynamicCatalog( - 'TestCatalog'))) + self.assertTrue(di.is_dynamic_catalog(di.DynamicCatalog())) class IsDeclarativeCatalogTests(unittest.TestCase): @@ -257,8 +256,7 @@ class IsDeclarativeCatalogTests(unittest.TestCase): def test_with_dynamic_catalog(self): """Test with dynamic catalog.""" - self.assertFalse(di.is_declarative_catalog(di.DynamicCatalog( - 'TestCatalog'))) + self.assertFalse(di.is_declarative_catalog(di.DynamicCatalog())) class IsCatalogBundleTests(unittest.TestCase):