Skip to main content

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.

Requires Python 3.10 or newer.

Features

  • Async resolve() and get_executor() APIs
  • Service lifetimes: transient, scoped, singleton
  • Multiple registrations per interface (resolve returns a list)
  • Ordered union resolution for typing.Union[...], A | B, and Optional[T]
  • Optional lifetime-compatibility warning (singleton depending on scoped)
  • Async context manager support for factories

Installation

pip install haldi

Haldi now targets Python 3.10+.

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)

Union annotations

Union-typed dependencies are resolved in declaration order. The first branch that can be resolved wins.

class Cache:
	...

class Repository:
	...

class Service:
	def __init__(self, dep: Cache | Repository | None):
		self.dep = dep

If Cache is registered, it will be injected. If not, Haldi tries Repository. If neither is registered, the final None branch is used.

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

Metadata

Release files for haldi 0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for haldi 0.7
File Size Uploaded Provenance Actions
haldi-0.7.tar.gz 9.6 kB Without provenance Details for haldi-0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for haldi 0.7
File Interpreter ABI Platform Actions
haldi-0.7-py3-none-any.whl Python 3 none any Details for haldi-0.7-py3-none-any.whl

Total release size: 19.8 kB

Release files / haldi-0.7.tar.gz

Download URL haldi-0.7.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a5c4a2e5f19f43ce59a9acc11ca009cd6191f709a5250d05988217bceee1aff6
BLAKE2b-256 checksum
How to use checksums
102fff4625ded2b4ef943e80d65733b324b890951c8fa60f063c712a89f987ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Release files / haldi-0.7-py3-none-any.whl

Download URL haldi-0.7-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f3a3c2827a7ebb0a62ba306850d08d761ba9e55f779da4cb476d2d190441c992
BLAKE2b-256 checksum
How to use checksums
67fae29ea08e1425299c82216307cb8a8968ff1c8b8c48c0789b46d5d0cfb3e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Security for haldi 0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page