Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_positional_arg_as_injectable_raises_type_error(self):
with pytest.raises(TypeError):
autowired(['b'])(foo)
def test_autowiring(self):
caller_args = (False, None, True)
caller_kwargs = {
'w': {},
'x': "string",
'f': lambda x: print(x),
'kwargs': {'extra': True},
}
_, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)
assert isinstance(kwargs['y'], bool)
assert isinstance(kwargs['z'], DummyClass)
assert isinstance(kwargs['l'], DummyClass)
assert isinstance(kwargs['s'], DummyClass)
def test_injectable_with_non_instantiable_class_raises_type_error(self):
# eligible injectable argument is annotated with non
# instantiable class
with pytest.raises(TypeError):
autowired()(baz)
with pytest.raises(TypeError):
autowired()(qux)
# specified argument for injection is annotated with non
# instantiable class
with pytest.raises(TypeError):
autowired(['nope'])(baz)
with pytest.raises(TypeError):
autowired(['nope'])(qux)
@autowired(lazy=True)
def __init__(self, *, dep1: CircularDependant1, dep2: CircularDependant2):
self.dep1 = dep1
self.dep2 = dep2
def test_missing_kwonly_args_raises_type_error(self):
with pytest.raises(TypeError):
autowired()(foo)(a=None, b=10, c='')
def test_injectable_lazy_initialization(self):
caller_args = (False, None, True)
caller_kwargs = {
'w': {},
'x': "string",
'f': lambda x: print(x),
'kwargs': {'extra': True},
}
_, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)
assert isinstance(kwargs['l'], Proxy)
assert isinstance(kwargs['l'], DummyClass)
assert kwargs['l'].dummy_method() == 42
def run_example():
load_injection_container()
example = InjectionContainerResetting()
example.run()
:param namespace: (optional) namespace in which the injectable will be registered.
Defaults to :const:`injectable.constants.DEFAULT_NAMESPACE`.
Usage::
>>> from injectable.testing import clear_injectables
>>> clear_injectables("foo")
.. versionadded:: 3.3.0
"""
namespace = InjectionContainer.NAMESPACES[namespace or DEFAULT_NAMESPACE]
if isinstance(dependency, str):
injectables = namespace.qualifier_registry[dependency]
namespace.qualifier_registry[dependency] = set()
else:
dependency_name = get_dependency_name(dependency)
injectables = namespace.class_registry[dependency_name]
namespace.class_registry[dependency_name] = set()
return injectables
def run(self):
self.bar()
# doing something
reset_injection_container()
try:
self.bar()
# WARNING:root:Injection Container is empty. Make sure \
# 'load_injection_container' is being called before any injections are made.
except InjectionError as e:
print(e)
# No injectable matches class 'Foo'
"""
Utility function to clear all injectables registered for the dependency in a given
namespace. Returns a set containing all cleared injectables.
:param dependency: class or qualifier of the dependency.
:param namespace: (optional) namespace in which the injectable will be registered.
Defaults to :const:`injectable.constants.DEFAULT_NAMESPACE`.
Usage::
>>> from injectable.testing import clear_injectables
>>> clear_injectables("foo")
.. versionadded:: 3.3.0
"""
namespace = InjectionContainer.NAMESPACES[namespace or DEFAULT_NAMESPACE]
if isinstance(dependency, str):
injectables = namespace.qualifier_registry[dependency]
namespace.qualifier_registry[dependency] = set()
else:
dependency_name = get_dependency_name(dependency)
injectables = namespace.class_registry[dependency_name]
namespace.class_registry[dependency_name] = set()
return injectables