Support stacked @inject decorators. Release 0.7.4.

Fixes #24.
This commit is contained in:
Alec Thomas 2013-07-11 10:26:24 -04:00
parent 4bb75cf0fb
commit 5edf27ee5f
2 changed files with 14 additions and 8 deletions

View File

@ -34,7 +34,7 @@ except AttributeError:
pass
__author__ = 'Alec Thomas <alec@swapoff.org>'
__version__ = '0.7.3'
__version__ = '0.7.4'
__version_tag__ = ''
# To enable get() tracing, getLogger('injector').setLevel(logging.INFO)
@ -611,15 +611,14 @@ class Injector(object):
:type kwargs: dict of string -> object
:return: Value returned by callable.
"""
bindings = getattr(callable, '__bindings__', {})
needed = dict(
(k, v) for (k, v) in bindings.items() if k not in kwargs)
bindings = getattr(callable, '__bindings__', None) or {}
needed = dict((k, v) for (k, v) in bindings.items() if k not in kwargs)
dependencies = self.args_to_inject(
function=callable,
bindings=needed,
owner_key=self_.__class__ if self_ is not None
else callable.__module__)
owner_key=self_.__class__ if self_ is not None else callable.__module__,
)
dependencies.update(kwargs)
@ -787,8 +786,11 @@ def inject(**bindings):
else:
inject = f
f.__bindings__ = bindings
inject.__bindings__ = bindings
function_bindings = getattr(f, '__bindings__', None) or {}
merged_bindings = dict(function_bindings, **bindings)
f.__bindings__ = merged_bindings
inject.__bindings__ = merged_bindings
return inject
def class_wrapper(cls):

4
pytest.ini Normal file
View File

@ -0,0 +1,4 @@
[pytest]
python_files = *test*.py
addopts = -v --tb=native --doctest-glob=*.md --doctest-modules injector.py injector_test.py README.md
norecursedirs = __pycache__ *venv* .git