Skip to main content

A dependency injection framework for Python with dynamic late-binding resolution

Project description

hdmi - Dependency Management Interface

Warning: Pre-Alpha Software

hdmi is experimental software in active development. Breaking changes may occur until version 1.0. Use with care in production environments.

A lightweight dependency injection framework for Python 3.13+ with:

  • Type-driven dependency discovery - Uses Python's standard type annotations
  • Scope-aware validation - Prevents lifetime bugs at build time
  • Lazy instantiation - Services created just-in-time
  • Early error detection - Configuration errors caught at build time

Quick Example

Simple Example (Singleton Services)

import asyncio
from hdmi import ContainerBuilder

# Define your services
class DatabaseConnection:
    def __init__(self):
        self.connected = True

class UserRepository:
    def __init__(self, db: DatabaseConnection):
        self.db = db

class UserService:
    def __init__(self, repo: UserRepository):
        self.repo = repo

async def main():
    # Configure the container (all singletons by default)
    builder = ContainerBuilder()
    builder.register(DatabaseConnection)
    builder.register(UserRepository)
    builder.register(UserService)

    # Build validates the dependency graph
    container = builder.build()

    # Resolve services lazily - dependencies are auto-wired!
    user_service = await container.get(UserService)

asyncio.run(main())

Using Scoped Services

import asyncio
from hdmi import ContainerBuilder

async def main():
    # For request-scoped services (e.g., web requests)
    builder = ContainerBuilder()
    builder.register(DatabaseConnection)  # singleton (default)
    builder.register(UserRepository, scoped=True)  # One per request
    builder.register(UserService, transient=True)   # New each time

    container = builder.build()

    # Scoped services must be resolved within a scope context
    async with container.scope() as scoped:
        user_service = await scoped.get(UserService)
        # All scoped dependencies share the same instance within this scope

asyncio.run(main())

Key Features

Two-Phase Architecture

  1. ContainerBuilder (Configuration): Register services and define scopes
  2. Container (Runtime): Validated, immutable graph for lazy resolution

Scope Safety

Services have four lifecycles that are validated at build time:

  • Singleton (default): One instance per container
  • Scoped: One instance per scope (e.g., per request)
  • Transient: New instance every time
  • Scoped Transient: New instance every time, requires scope

Validation Rules (Simplified): The only invalid dependency is when a non-scoped service (singleton or transient) depends on a scoped service.

# Valid: Scoped -> Singleton
builder = ContainerBuilder()
builder.register(DatabaseConnection)  # singleton (default)
builder.register(UserRepository, scoped=True)

# Invalid: Singleton -> Scoped (raises ScopeViolationError)
builder = ContainerBuilder()
builder.register(RequestHandler, scoped=True)
builder.register(SingletonService)  # singleton depends on scoped!
container = builder.build()  # ScopeViolationError!

Type-Driven Dependencies

Dependencies are automatically discovered from type annotations:

class ServiceA:
    def __init__(self, dep: DependencyType):
        self.dep = dep

No decorators or manual wiring required!

Installation

pip install hdmi  # Coming soon

Development

This project uses uv for dependency management and follows strict TDD methodology.

# Run all checks (linting, type checking, tests)
make test

# Build documentation
make docs

# See all available commands
make help

License

MIT License - see LICENSE file for details.

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

hdmi-0.2.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

hdmi-0.2.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file hdmi-0.2.1.tar.gz.

File metadata

  • Download URL: hdmi-0.2.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hdmi-0.2.1.tar.gz
Algorithm Hash digest
SHA256 64c0fc7c1ba80490fca3ab0013358a666f34ace3a1cf81b5e1c1d641e7acf1a5
MD5 da42bbaadab6a55f7ea91322fb7f6454
BLAKE2b-256 879485e263b441ef8daf2ae3568edf1c96c27563c2da59713fe8fa7714fbee47

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdmi-0.2.1.tar.gz:

Publisher: cicd.yml on msqd/hdmi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hdmi-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: hdmi-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hdmi-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 901b9bca60deda41066103db836473c558a3c13a78914ca98183be8525f1b0bd
MD5 192477dab913668fbf47c269bfc4ca02
BLAKE2b-256 f40e2ab321b19ebfb4562f8eb2fce860dafdc95120e2cf231d9d982a68bfce89

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdmi-0.2.1-py3-none-any.whl:

Publisher: cicd.yml on msqd/hdmi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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