Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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
>>> from injectable import Injectable
>>> from injectable.testing import register_injectables
>>> injectable = Injectable(constructor=lambda: 42)
>>> register_injectables({injectable}, qualifier="foo")
.. versionadded:: 3.3.0
"""
if not klass and not qualifier:
raise ValueError(
"At least one of 'klass' or 'qualifier' parameters must to be defined"
)
if propagate and not klass:
raise ValueError(
"When 'propagate' is True the parameter 'klass' must be defined"
)
namespace = InjectionContainer.NAMESPACES[namespace or DEFAULT_NAMESPACE]
for injectable in injectables:
namespace.register_injectable(injectable, klass, qualifier, propagate)