Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
: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 _register_to_class(
self, klass: type, injectable: Injectable,
):
qualified_name = get_dependency_name(klass)
if qualified_name not in self.class_registry:
self.class_registry[qualified_name] = set()
self.class_registry[qualified_name].add(injectable)
matches the qualifier/class and group inside the specified namespace instead
of raising an :class:`InjectionError `.
Ambiguous cases where resolving a primary injectable is impossible will
still raise :class:`InjectionError `.
Defaults to False.
Usage::
>>> from foo import Foo
>>> from injectable import inject
>>>
>>> class Bar:
... def __init__(self, foo: Foo = None):
... self.foo = foo or inject(Foo)
"""
dependency_name = get_dependency_name(dependency)
registry_type = get_dependency_registry_type(dependency)
matches = get_namespace_injectables(
dependency_name, registry_type, namespace or DEFAULT_NAMESPACE
)
if not matches:
if not optional:
raise InjectionError(
f"No injectable matches {registry_type.value} '{dependency_name}'"
)
return None
if group is not None or exclude_groups is not None:
matches = filter_by_group(matches, group, exclude_groups)
if not matches:
if not optional:
raise InjectionError(
f"No injectable for {registry_type.value} '{dependency_name}'"
False.
:param optional: (optional) when True this function returns an empty list if no
injectable matches the qualifier/class and group inside the specified
namespace. Defaults to False.
Usage::
>>> from com import AbstractService
>>> from injectable import inject_multiple
>>> from typing import Sequence
>>>
>>> class Foo:
... def __init__(self, services: Sequence[AbstractService] = None):
... self.services = services or inject_multiple(AbstractService)
"""
dependency_name = get_dependency_name(dependency)
registry_type = get_dependency_registry_type(dependency)
matches = get_namespace_injectables(
dependency_name, registry_type, namespace or DEFAULT_NAMESPACE
)
if not matches:
if not optional:
raise InjectionError(
f"No injectable matches {registry_type.value} '{dependency_name}'"
)
return []
if group is not None or exclude_groups is not None:
matches = filter_by_group(matches, group, exclude_groups,)
if not matches:
if not optional:
raise InjectionError(
f"No injectable for {registry_type.value} '{dependency_name}'"