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.0.tar.gz (10.3 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.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hdmi-0.2.0.tar.gz
  • Upload date:
  • Size: 10.3 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.0.tar.gz
Algorithm Hash digest
SHA256 ad458603a0fddff8929512850cc52080e7ab434698e92fb57d7b0c6803d690f1
MD5 940e8c4d27484468ad411848e9fb61fb
BLAKE2b-256 0d5778f7cc0e15bed825a39c4b5df0bd807c4234ab2081fedd79e65ba47217e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdmi-0.2.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: hdmi-0.2.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9eb4c20f33f7d32a6e270857b238f42dc04bdfc37663bab8e6869be10c2f265a
MD5 e3aa012a1c022e30adbfe100272dc7d4
BLAKE2b-256 1092d865ce44d12b60c3d94442bc565fc1630c4ed8c4fe56b3bf1859e1db4c72

See more details on using hashes here.

Provenance

The following attestation bundles were made for hdmi-0.2.0-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