Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Examples:
If we need to register a dependency, but we don't need to
abstract it, we can register it as concrete.
>>> from punq import Container
>>> container = Container()
>>> class FileReader:
... def read(self):
... # Assorted legerdemain and rigmarole
... pass
...
>>> container.register(FileReader)
"""
if not inspect.isclass(service):
raise InvalidRegistrationException(
"The service %s can't be registered as its own implementation"
% (repr(service))
)
self.__registrations[service].append(
Registration(service, scope, service, self._get_needs_for_ctor(service), {})
)
def register(
self, service, factory=empty, instance=empty, scope=Scope.transient, **kwargs
):
resolve_args = kwargs or {}
if instance is not empty:
self.register_service_and_instance(service, instance)
elif factory is empty:
self.register_concrete_service(service, scope)
elif callable(factory):
self.register_service_and_impl(service, scope, factory, resolve_args)
else:
raise InvalidRegistrationException(
f"Expected a callable factory for the service {service} but received {factory}"
)
self._update_localns(service)
ensure_forward_ref(self, service, factory, instance, **kwargs)