Raise when marking a non-existent argument as noninjectable

Fixes #92.
This commit is contained in:
David Pärsson 2018-09-19 09:58:53 +02:00 committed by Jakub Stasiak
parent b14b4cc359
commit 718892f145
2 changed files with 21 additions and 1 deletions

View File

@ -114,6 +114,10 @@ class UnknownProvider(Error):
"""Tried to bind to a type whose provider couldn't be determined."""
class UnknownArgument(Error):
"""Tried to mark an unknown argument as noninjectable."""
class Provider:
"""Provides class instances."""
@ -1071,6 +1075,12 @@ def noninjectable(*args):
doesn't matter.
"""
def decorator(function):
bindings = _infer_injected_bindings(function)
for arg in args:
if arg not in bindings:
raise UnknownArgument('Unable to mark unknown argument %s '
'as non-injectable.' % arg)
existing = getattr(function, '__noninjectables__', set())
merged = existing | set(args)
function.__noninjectables__ = merged

View File

@ -25,7 +25,7 @@ from injector import (
CircularDependency, Module, Key, SingletonScope,
ScopeDecorator, with_injector, AssistedBuilder, BindingKey,
SequenceKey, MappingKey, provider, ProviderOf, ClassAssistedBuilder,
Error,
Error, UnknownArgument,
)
@ -1109,6 +1109,16 @@ def test_assisted_building_is_supported():
assert processor.name == 'John'
def test_raises_when_noninjectable_arguments_defined_with_invalid_arguments():
with pytest.raises(UnknownArgument):
class A:
@inject
@noninjectable('c')
def __init__(self, b: str):
self.b = b
def test_implicit_injection_fails_when_annotations_are_missing():
class A:
def __init__(self, n):