mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Remove ExternalDependency.satisfy() in favor of inherited Provider.override()
This commit is contained in:
parent
3b880d12a5
commit
27bf9e0a8e
|
@ -49,7 +49,7 @@ class Catalog(AbstractCatalog):
|
||||||
|
|
||||||
|
|
||||||
# Satisfaction of external dependency.
|
# Satisfaction of external dependency.
|
||||||
Catalog.database.satisfy(Singleton(sqlite3.Connection,
|
Catalog.database.override(Singleton(sqlite3.Connection,
|
||||||
InitArg('database', ':memory:'),
|
InitArg('database', ':memory:'),
|
||||||
Attribute('row_factory', sqlite3.Row)))
|
Attribute('row_factory', sqlite3.Row)))
|
||||||
|
|
||||||
|
|
|
@ -181,26 +181,25 @@ class Scoped(NewInstance):
|
||||||
|
|
||||||
class ExternalDependency(Provider):
|
class ExternalDependency(Provider):
|
||||||
|
|
||||||
"""External dependency provider."""
|
"""External dependency provider.
|
||||||
|
|
||||||
__slots__ = ('instance_of', 'dependency')
|
Those provider is used when dependency obviously have to be overridden by
|
||||||
|
the client's code, but it's interface is known.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__slots__ = ('instance_of',)
|
||||||
|
|
||||||
def __init__(self, instance_of):
|
def __init__(self, instance_of):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.instance_of = instance_of
|
self.instance_of = instance_of
|
||||||
self.dependency = None
|
|
||||||
super(ExternalDependency, self).__init__()
|
super(ExternalDependency, self).__init__()
|
||||||
|
|
||||||
def satisfy(self, provider):
|
|
||||||
"""Satisfy an external dependency."""
|
|
||||||
self.dependency = ensure_is_provider(provider)
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
"""Return provided instance."""
|
"""Return provided instance."""
|
||||||
if not self.dependency:
|
if not self.overridden:
|
||||||
raise Error('Dependency is not satisfied')
|
raise Error('Dependency is not defined')
|
||||||
|
|
||||||
instance = self.dependency.__call__(*args, **kwargs)
|
instance = self.last_overriding(*args, **kwargs)
|
||||||
|
|
||||||
if not isinstance(instance, self.instance_of):
|
if not isinstance(instance, self.instance_of):
|
||||||
raise Error('{0} is not an '.format(instance) +
|
raise Error('{0} is not an '.format(instance) +
|
||||||
|
|
|
@ -407,17 +407,17 @@ class ExternalDependencyTests(unittest.TestCase):
|
||||||
"""Test `is_provider` check."""
|
"""Test `is_provider` check."""
|
||||||
self.assertTrue(is_provider(self.provider))
|
self.assertTrue(is_provider(self.provider))
|
||||||
|
|
||||||
def test_call_satisfied(self):
|
def test_call_overridden(self):
|
||||||
"""Test call of satisfied external dependency."""
|
"""Test call of overridden external dependency."""
|
||||||
self.provider.satisfy(NewInstance(list))
|
self.provider.override(NewInstance(list))
|
||||||
self.assertIsInstance(self.provider(), list)
|
self.assertIsInstance(self.provider(), list)
|
||||||
|
|
||||||
def test_call_satisfied_but_not_instance_of(self):
|
def test_call_overridden_but_not_instance_of(self):
|
||||||
"""Test call of satisfied external dependency, but not instance of."""
|
"""Test call of overridden external dependency, but not instance of."""
|
||||||
self.provider.satisfy(NewInstance(dict))
|
self.provider.override(NewInstance(dict))
|
||||||
self.assertRaises(Error, self.provider)
|
self.assertRaises(Error, self.provider)
|
||||||
|
|
||||||
def test_call_not_satisfied(self):
|
def test_call_not_overridden(self):
|
||||||
"""Test call of not satisfied external dependency."""
|
"""Test call of not satisfied external dependency."""
|
||||||
self.assertRaises(Error, self.provider)
|
self.assertRaises(Error, self.provider)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user