mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
0.2.1, ExternalDependency can take several types
This commit is contained in:
parent
92254b0276
commit
1631978790
|
@ -2,6 +2,7 @@
|
||||||
Standard providers.
|
Standard providers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from collections import Iterable
|
||||||
from .injections import InitArg, Attribute, Method
|
from .injections import InitArg, Attribute, Method
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,6 +120,8 @@ class ExternalDependency(Provider):
|
||||||
|
|
||||||
:param instance_of: type
|
:param instance_of: type
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(instance_of, Iterable):
|
||||||
|
instance_of = (instance_of,)
|
||||||
self.instance_of = instance_of
|
self.instance_of = instance_of
|
||||||
self.dependency = None
|
self.dependency = None
|
||||||
super(ExternalDependency, self).__init__()
|
super(ExternalDependency, self).__init__()
|
||||||
|
@ -140,9 +143,9 @@ class ExternalDependency(Provider):
|
||||||
|
|
||||||
result = self.dependency.__call__(*args, **kwargs)
|
result = self.dependency.__call__(*args, **kwargs)
|
||||||
|
|
||||||
if not isinstance(result, self.instance_of):
|
if not any((isinstance(result, possible_type) for possible_type in self.instance_of)):
|
||||||
raise TypeError('{} is not an instance of {}'.format(result,
|
raise TypeError('{} is not an instance of {}'.format(result, self.instance_of))
|
||||||
self.instance_of))
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user