Skip to main content

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

Project description

hdmi - Dependency Management Interface

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)

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

# 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 = container.get(UserService)

Using Scoped Services

# 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
with container.scope() as scoped:
    user_service = scoped.get(UserService)
    # All scoped dependencies share the same instance within this scope

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)

# L 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.1.0.tar.gz (9.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.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hdmi-0.1.0.tar.gz
  • Upload date:
  • Size: 9.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.1.0.tar.gz
Algorithm Hash digest
SHA256 a59b2f01098f90d4257da5b804c3c3b79542c0be52945c7a99961cdbaaeb0847
MD5 5986670ef589111e171fd960bab5d70d
BLAKE2b-256 93d671028f941f27fd0da9553ad5d44fe162adca745658c4472ae3f8d4f2d79f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: hdmi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eed1ac33370d1f61096641511866fcca735810c0327dc480679556423b1e7782
MD5 f151a6289767a902fcd4f62ed7e43e40
BLAKE2b-256 76aeef29bf2e36b2d9f231059ee58cb6e5cb10f8f36f1b198ba3c77d590dd398

See more details on using hashes here.

Provenance

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