Skip to main content

Lightweight dependency injection container for Python.

Project description

philiprehberger-di

Tests PyPI version Last updated

Lightweight dependency injection container for Python.

Installation

pip install philiprehberger-di

Usage

from philiprehberger_di import Container

container = Container()
container.register(Logger)
logger = container.resolve(Logger)

Singletons

container.register(Database, singleton=True)

a = container.resolve(Database)
b = container.resolve(Database)
assert a is b

Custom Factories

container.register(Cache, factory=lambda: Cache(max_size=256))
cache = container.resolve(Cache)

Recursive Resolution

class Service:
    def __init__(self, db: Database, logger: Logger) -> None:
        self.db = db
        self.logger = logger

container.register(Database)
container.register(Logger)
container.register(Service)
service = container.resolve(Service)  # db and logger are injected automatically

Inject Decorator

from philiprehberger_di import Container, inject

container = Container()
container.register(Logger, singleton=True)

@inject(container)
def handle_request(logger: Logger) -> str:
    logger.log("request handled")
    return "ok"

handle_request()  # logger is resolved and injected automatically

Lifecycle Hooks

container.register(
    Database,
    singleton=True,
    on_create=lambda db: db.connect(),
    on_destroy=lambda db: db.disconnect(),
)

db = container.resolve(Database)  # on_create called after creation
container.reset()                 # on_destroy called before clearing singletons

Circular Dependency Detection

The container detects circular dependencies during resolution and raises a CircularDependencyError with the full dependency chain:

from philiprehberger_di import CircularDependencyError

class A:
    def __init__(self, b: B) -> None: ...

class B:
    def __init__(self, a: A) -> None: ...

container.register(A)
container.register(B)

try:
    container.resolve(A)
except CircularDependencyError as e:
    print(e)  # Circular dependency detected: A -> B -> A
    print(e.chain)  # [A, B, A]

Scoped Lifetime

Services registered with Lifetime.SCOPED are singletons within a scope but differ across scopes:

from philiprehberger_di import Container, Lifetime

container = Container()
container.register(RequestContext, lifetime=Lifetime.SCOPED)
container.register(Logger, lifetime=Lifetime.SINGLETON)

with container.create_scope() as scope:
    ctx1 = scope.resolve(RequestContext)
    ctx2 = scope.resolve(RequestContext)
    assert ctx1 is ctx2  # same within the scope

# on_destroy hooks are called when the scope exits

API

Function / Class Description
Container() Create a new dependency injection container
container.register(cls, factory?, singleton?, lifetime?, on_create?, on_destroy?) Register a class with optional factory, lifetime, and lifecycle hooks
container.resolve(cls) Resolve an instance, recursively injecting dependencies
container.create_scope() Create a child scope for scoped lifetime management
container.reset() Call on_destroy for singletons with hooks, then clear the cache
inject(container) Decorator that resolves type-hinted params from the container
Lifetime.TRANSIENT New instance on every resolve (default)
Lifetime.SINGLETON Single shared instance across the container
Lifetime.SCOPED Single instance per scope, transient without a scope
CircularDependencyError Raised when a circular dependency chain is detected
Scope Child scope returned by create_scope(), used as a context manager

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_di-0.3.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_di-0.3.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_di-0.3.1.tar.gz.

File metadata

  • Download URL: philiprehberger_di-0.3.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_di-0.3.1.tar.gz
Algorithm Hash digest
SHA256 027ab004398248b76eaed03b49613fe9e2a08f5c305571b6f05f28b4d3d307ce
MD5 869e95c5f92e09edd857fcd55d12311d
BLAKE2b-256 75908a6ec26e4bca8dfdfa4f0faffdd7b7a45fcbb091e4cfb8a9ea00e6ff1d50

See more details on using hashes here.

File details

Details for the file philiprehberger_di-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_di-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7a8019d358daaa4db3e9b6daa094e675203edfb53e0dc0d0c6794ed77ef6ae5
MD5 95eeadf2f598798106b18500e4667ab4
BLAKE2b-256 66819184f59db5ca02e0eee4854d7aec8e819ffaf9e95251444dd6af1c9406fb

See more details on using hashes here.

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