Skip to main content

A simple and efficient Python library for handling dependency injection.

Project description

haldi logo

haldi

Async-first dependency injection container for Python. It supports transient, scoped, and singleton lifetimes, resolves constructor (or callable) dependencies via type annotations, and can manage async context managers during resolution.

Features

  • Async resolve() and get_executor() APIs
  • Service lifetimes: transient, scoped, singleton
  • Multiple registrations per interface (resolve returns a list)
  • Optional lifetime-compatibility warning (singleton depending on scoped)
  • Async context manager support for factories

Installation

pip install haldi

Quick start

from haldi import Container

class Repo:
	...

class Service:
	def __init__(self, repo: Repo):
		self.repo = repo

container = Container()
container.add_transient(Repo)
container.add_scoped(Service)

# Resolve inside an async context
service = await container.get(Service)

Service lifetimes

  • Transient: a new instance every time.
  • Scoped: one instance per scope. Use create_scope() or scoped().
  • Singleton: one instance for the container lifetime.
container = Container()
container.add_transient(Repo)
container.add_scoped(Service)
container.add_singleton(Config)

scope = container.create_scope()
svc_a = await scope.get(Service)
svc_b = await scope.get(Service)
assert svc_a is svc_b

Multiple implementations

Register multiple providers for the same interface and resolve() returns a list.

class Interface:
	...

class ImplA(Interface):
	...

class ImplB(Interface):
	...

container.add_transient(Interface, ImplA)
container.add_transient(Interface, ImplB)

instances = await container.resolve(Interface)

Factory registration

Factories can be callables (sync or async). Return annotations are required unless using a lambda.

async def build_client() -> Client:
	return Client()

container.add_singleton(Client, build_client)

Scoped context helper

Use scoped() to automatically close async context managers created during resolution.

async with container.scoped() as scope:
	service = await scope.get(Service)

Warnings

If a singleton depends on a scoped service, a UserWarning is emitted because the scoped instance would be captured for the lifetime of the singleton.

API overview

  • Container.add_transient(type, concrete_type=None)
  • Container.add_scoped(type, concrete_type=None)
  • Container.add_singleton(type, concrete_type=None)
  • await Container.resolve(type, strict=True)
  • await Container.get(type)
  • await Container.try_get(type)
  • await Container.get_executor(callable, strict=True)
  • Container.create_scope()
  • Container.scoped()

Development

pip install -e .
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haldi-0.6.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

haldi-0.6-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file haldi-0.6.tar.gz.

File metadata

  • Download URL: haldi-0.6.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for haldi-0.6.tar.gz
Algorithm Hash digest
SHA256 45737866bc0d402633210d2fcc4af457a55b40f948bc5f2fa33e58f1b4bc9cc9
MD5 92fb3a64898e0a822df19401d75e4184
BLAKE2b-256 191f5047114dbe36d772a9ab08335b198c32208d68237dc6df6b51be3ca3a20e

See more details on using hashes here.

File details

Details for the file haldi-0.6-py3-none-any.whl.

File metadata

  • Download URL: haldi-0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for haldi-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7e1e74b3b88bdb8ef586965092e4acf8adb126c88dec4f810ed535bc494a044d
MD5 5086837835dde75e48f2c39ad4ea2b3f
BLAKE2b-256 0bf87579f872043efbbfdbbcd09080cfa7d13b62331eefc74d945c11d369085f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page