Restrict overriding of providers with themselves

This commit is contained in:
Roman Mogilatov 2015-11-20 13:51:36 +02:00
parent 176ff06778
commit 99b9a5648a
2 changed files with 7 additions and 0 deletions

View File

@ -46,6 +46,9 @@ class Provider(object):
def override(self, provider): def override(self, provider):
"""Override provider with another 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: if not self.is_overridden:
self.overridden_by = (ensure_is_provider(provider),) self.overridden_by = (ensure_is_provider(provider),)
else: else:

View File

@ -65,6 +65,10 @@ class ProviderTests(unittest.TestCase):
self.provider.override(overriding_provider) self.provider.override(overriding_provider)
self.assertTrue(self.provider.is_overridden) 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): def test_override_with_not_provider(self):
"""Test provider overriding with not provider instance.""" """Test provider overriding with not provider instance."""
self.assertRaises(di.Error, self.provider.override, object()) self.assertRaises(di.Error, self.provider.override, object())