From 99b9a5648a814e9cfbda2a42d2e32dc6bd00e1bc Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 20 Nov 2015 13:51:36 +0200 Subject: [PATCH] Restrict overriding of providers with themselves --- dependency_injector/providers.py | 3 +++ tests/test_providers.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/dependency_injector/providers.py b/dependency_injector/providers.py index 22595481..f9d8cd30 100644 --- a/dependency_injector/providers.py +++ b/dependency_injector/providers.py @@ -46,6 +46,9 @@ class Provider(object): def override(self, provider): """Override provider with another provider.""" + if provider is self: + raise Error('Provider {0} could not be overridden ' + 'with itself'.format(self)) if not self.is_overridden: self.overridden_by = (ensure_is_provider(provider),) else: diff --git a/tests/test_providers.py b/tests/test_providers.py index 2ce35bba..81afbe90 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -65,6 +65,10 @@ class ProviderTests(unittest.TestCase): self.provider.override(overriding_provider) self.assertTrue(self.provider.is_overridden) + def test_override_with_itself(self): + """Test provider overriding with itself.""" + self.assertRaises(di.Error, self.provider.override, self.provider) + def test_override_with_not_provider(self): """Test provider overriding with not provider instance.""" self.assertRaises(di.Error, self.provider.override, object())