Python Dependency Injection Framework
Project description
Python Dependency Injection Framework
A lightweight, type-safe dependency injection framework for Python that supports singleton, lazy singleton, and factory registrations, with scoped overrides and flexible parameter injection.
Features
- Registry-based DI: Register services and resolve them by type and optional tags.
- Scoped overrides: Create temporary overrides using context managers.
- Injection decorator: Automatically inject dependencies into functions, methods, or constructors.
- Multiple registration modes:
SINGLETON: Single instance shared globally.LAZY_SINGLETON: Created on first access and then cached.FACTORY: New instance created on every resolution.
- Tag-based resolution: Support multiple implementations of the same interface.
- Type-safe: Uses Python type annotations for automatic injection.
- Supports instance, class, and static methods.
Usage
Using the Global Locator
The package provides a pre-created global locator available from the module:
from di import locator, inject, RegistrationMode, NotRegisteredError
You can register services directly in the global locator:
from di import locator, RegistrationMode
class Service:
pass
locator.register(Service, lambda: Service(), RegistrationMode.SINGLETON)
locator.register(int, lambda: 42, RegistrationMode.SINGLETON, tag="answer")
Resolve dependencies manually:
service_instance = locator.resolve(Service)
number = locator.resolve(int, tag="answer")
Function and Method Injection with the Global Locator
from di import locator, inject
@inject(locator, params=["service", "num:answer"])
def process(service: Service, num: int):
return service, num
s, n = process()
Supports injection into:
-
Instance methods:
class MyClass: @inject(locator, params=["service"]) def method(self, service: Service): return service
-
Class methods:
class MyClass: @classmethod @inject(locator, params=["service"]) def method(cls, service: Service): return service, cls
-
Static methods:
class MyClass: @staticmethod @inject(locator, params=["service"]) def method(service: Service): return service
-
Constructors:
class MyClass: @inject(locator, params=["service"]) def __init__(self, service: Service): self.service = service
Scoped Overrides
with locator.override():
locator.register(Service, lambda: CustomService(), RegistrationMode.SINGLETON)
instance = locator.resolve(Service) # Returns CustomService
# Outside override, original registration restored
instance = locator.resolve(Service) # Returns Service
Registration Modes
| Mode | Behavior |
|---|---|
SINGLETON |
Single instance created at registration and reused for all resolutions. |
LAZY_SINGLETON |
Instance created on first resolution, then cached for subsequent calls. |
FACTORY |
Factory function called on every resolution to return a new instance. |
Error Handling
NotRegisteredError: Raised when a requested type or tag is not registered.RootScopeCloseError: Raised when attempting to close the root scope of aLocator.
Advanced Usage
While the global locator is sufficient for most cases, you can create custom locators or registries if you need isolated scopes or multiple independent DI containers:
from di import Locator, Registry
my_locator = Locator()
my_registry = Registry()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydi_framework-1.0.0.tar.gz.
File metadata
- Download URL: pydi_framework-1.0.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95b9534af93ce04416a88f852a9a8aa0af450a5cff029c5c80a23a45df99ad43
|
|
| MD5 |
2480ccf37e9b208b4272c3461367b8d0
|
|
| BLAKE2b-256 |
41ba56b9728a95d0ad91f66d497c8f4710ec81e7f3b147bc704ce3f66d25a3f7
|
Provenance
The following attestation bundles were made for pydi_framework-1.0.0.tar.gz:
Publisher:
release.yaml on mounirmelzi/pydi-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydi_framework-1.0.0.tar.gz -
Subject digest:
95b9534af93ce04416a88f852a9a8aa0af450a5cff029c5c80a23a45df99ad43 - Sigstore transparency entry: 1208088137
- Sigstore integration time:
-
Permalink:
mounirmelzi/pydi-framework@74e168e0b6db7d6a772e68a192722ea35d6d019f -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/mounirmelzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@74e168e0b6db7d6a772e68a192722ea35d6d019f -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydi_framework-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pydi_framework-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fb2ae55e97d6763ab5eb70e9483e03f89ce363fc42009809aa6e26d76dee803
|
|
| MD5 |
6c5b7199e15258b369906a710ec79bbc
|
|
| BLAKE2b-256 |
a8e4625882ed06b4a65157fa3b0a8001c8810f234022eac8a399ec80aff5889e
|
Provenance
The following attestation bundles were made for pydi_framework-1.0.0-py3-none-any.whl:
Publisher:
release.yaml on mounirmelzi/pydi-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydi_framework-1.0.0-py3-none-any.whl -
Subject digest:
0fb2ae55e97d6763ab5eb70e9483e03f89ce363fc42009809aa6e26d76dee803 - Sigstore transparency entry: 1208088210
- Sigstore integration time:
-
Permalink:
mounirmelzi/pydi-framework@74e168e0b6db7d6a772e68a192722ea35d6d019f -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/mounirmelzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@74e168e0b6db7d6a772e68a192722ea35d6d019f -
Trigger Event:
push
-
Statement type: