Skip to content

Commit fd4ee33

Browse files
committed
Be explicit about requiring keyword arguments
1 parent 8162456 commit fd4ee33

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

injector.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,13 @@ def class_wrapper(cls):
814814

815815
@inject(**bindings)
816816
def init(self, *args, **kwargs):
817-
for key in original_keys:
818-
setattr(self, key, kwargs.pop(key.lstrip('_')))
817+
try:
818+
for key in original_keys:
819+
normalized_key = key.lstrip('_')
820+
setattr(self, key, kwargs.pop(normalized_key))
821+
except KeyError as e:
822+
reraise(e, CallError('Keyword argument %s not found' % normalized_key))
823+
819824
orig_init(self, *args, **kwargs)
820825
cls.__init__ = init
821826
return cls

injector_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,17 @@ class X(object):
987987
x = X(b=314)
988988
assert (x._b == 314)
989989

990+
def test_correct_exception_is_raised_when_argument_is_missing(self):
991+
@inject(s=str)
992+
class X(object):
993+
pass
994+
995+
with pytest.raises(CallError):
996+
self.B()
997+
998+
with pytest.raises(CallError):
999+
self.B('something')
1000+
9901001

9911002
def test_provides_and_scope_decorator_collaboration():
9921003
@provides(int)

0 commit comments

Comments
 (0)