Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'
def resolve_single_injectable(
dependency_name: str, registry_type: RegistryType, matches: Set[Injectable]
) -> Injectable:
if len(matches) == 1:
for injectable in matches:
return injectable
primary_matches = [inj for inj in matches if inj.primary]
if len(primary_matches) == 0:
raise InjectionError(
f"No primary injectable registered for {registry_type.value}:"
f" '{dependency_name}'. Unable to resolve unambiguously: {len(matches)}"
f" possible matches."
)
if len(primary_matches) > 1:
raise InjectionError(
f"Found {len(primary_matches)} injectables registered as primary for "
f"{registry_type.value}: '{dependency_name}'. Unable to resolve"
f" unambiguously."
)
return primary_matches[0]
def resolve_single_injectable(lookup_key, lookup_type, matches) -> Injectable:
if len(matches) == 1:
for injectable in matches:
break
else:
primary_matches = [inj for inj in matches if inj.primary]
if len(primary_matches) == 0:
raise InjectionError(
f"No primary injectable registered for {lookup_type}: '{lookup_key}'. "
f"Unable to resolve unambiguously: {len(matches)} possible matches."
)
if len(primary_matches) > 1:
raise InjectionError(
f"Found {len(primary_matches)} injectables registered as primary for "
f"{lookup_type}: '{lookup_key}'. Unable to resolve unambiguously."
)
injectable = primary_matches[0]
return injectable
>>> 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}'"
f" matches group '{group}'"
)
return None
injectable = resolve_single_injectable(dependency_name, registry_type, matches)
return injectable.get_instance(lazy=lazy)
>>> 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}'"
f" matches group '{group}'"
)
return []
return [inj.get_instance(lazy=lazy) for inj in matches]