Add returning of overriding provider in provider overriding context

This commit is contained in:
Roman Mogilatov 2016-03-14 00:08:41 +02:00
parent 2f50fcb9e7
commit d51f2fa79a
1 changed files with 13 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import six
from .injections import _parse_args_injections from .injections import _parse_args_injections
from .injections import _parse_kwargs_injections from .injections import _parse_kwargs_injections
from .utils import is_provider
from .utils import ensure_is_provider from .utils import ensure_is_provider
from .utils import is_attribute_injection from .utils import is_attribute_injection
from .utils import is_method_injection from .utils import is_method_injection
@ -117,6 +118,10 @@ class Provider(object):
if provider is self: if provider is self:
raise Error('Provider {0} could not be overridden ' raise Error('Provider {0} could not be overridden '
'with itself'.format(self)) 'with itself'.format(self))
if not is_provider(provider):
provider = Object(provider)
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:
@ -126,7 +131,7 @@ class Provider(object):
if self.__class__.__OPTIMIZED_CALLS__: if self.__class__.__OPTIMIZED_CALLS__:
self.__call__ = self.provide = self._call_last_overriding self.__call__ = self.provide = self._call_last_overriding
return OverridingContext(self) return OverridingContext(self, provider)
def reset_last_overriding(self): def reset_last_overriding(self):
"""Reset last overriding provider. """Reset last overriding provider.
@ -1056,16 +1061,21 @@ class OverridingContext(object):
assert not provider.is_overridden assert not provider.is_overridden
""" """
def __init__(self, overridden): def __init__(self, overridden, overriding):
"""Initializer. """Initializer.
:param overridden: Overridden provider :param overridden: Overridden provider.
:type overridden: :py:class:`Provider` :type overridden: :py:class:`Provider`
:param overriding: Overriding provider.
:type overriding: :py:class:`Provider`
""" """
self.overridden = overridden self.overridden = overridden
self.overriding = overriding
def __enter__(self): def __enter__(self):
"""Do nothing.""" """Do nothing."""
return self.overriding
def __exit__(self, *_): def __exit__(self, *_):
"""Exit overriding context.""" """Exit overriding context."""