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.0rc3.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.0rc3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hdmi-0.1.0rc3.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.0rc3.tar.gz
Algorithm Hash digest
SHA256 d4bc6a6e2f79c438fa0686a59060ea2e52d6927256858a20d4ab2aebab746835
MD5 c937a60091e812827d51e16a47e483f6
BLAKE2b-256 e22ea4d8b3d7ecf69b200e63eb8c35526597656e5f5c799dddbdfac6c752a9c3

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: hdmi-0.1.0rc3-py3-none-any.whl
  • Upload date:
  • Size: 14.7 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.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 1dedbbba0a1dd557ffeff1be5051ea406f6b0743a80401d23a3896eb047b53fe
MD5 c67f2c689effbbf7132671986609b520
BLAKE2b-256 de7969b14e2a2c792a7864c40b474e9b02b65bc0603e74124439a354763e7a5c

See more details on using hashes here.

Provenance

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