Skip to content

Service locator injection (ARCH2198)

Key python.quality.service-locator · Severity warning · Category quality

Injecting the container/provider and resolving services from it hides dependencies and hurts testability. Inject dependencies directly through the constructor.

class OrderService:
def __init__(self, container):
self.container = container
def run(self):
repo = self.container.resolve("repo")
repo.save()
class OrderService:
def __init__(self, repo):
self.repo = repo
def run(self):
self.repo.save()

Inject concrete dependencies explicitly through the constructor instead of the container.