Python Injection Framework (PIF)
A simple Python dependency injection framework.
Usage
You can install this project from pypi.
pip install python-injection-framework
Dependency Injection
The injection framework is configured to inject any default values for method arguments that are instances
of providers.Provider.
This implementation works by wrapping decorators around methods any patching any unfilled providers.Provider default
arguments at runtime.
All dependency injection is lazily evaluated so providers are only evaluated when a method is called. This approach is optimal as it reduces necessary computation for expensive services and reduces
Decorator Injection
With this approach you can automatically inject functions at load time using the @wiring.inject decorator.
from scottzach1.pif import providers
from scottzach1.pif import wiring
@wiring.inject # <- automatically injects providers.Provider default arguments!
def my_function(a: str = providers.ExistingSingleton("hello world")):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
Module Injection
With this approach you can wire all methods in the specified modules.
from scottzach1.pif import providers
from scottzach1.pif import wiring
def my_function(a: str = providers.ExistingSingleton("hello world")):
return a
if __name__ == "__main__":
wiring.wire([__name__]) # <- dynamically inject methods with providers.Provider default arguments!
assert "hello world" == my_function()
Overriding
This package provides a simple mechanism to override providers. This can be very useful when it comes to mocking services for testing or dynamically patching application behavior based on application configuration.
Standard Overriding
If you want to patch a value all you need to do is call .override() on the provider in question. If you are wanting to
override an existing singleton you may call the convenience method .override_existing().
from scottzach1.pif import providers
from scottzach1.pif import wiring
StringProvider = providers.ExistingSingleton("hello world")
@wiring.inject
def my_function(a: str = StringProvider):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
override = StringProvider.override_existing("overridden_1")
assert "overridden_1"
Context Managers
If you want more control around the override lifecycles then you may use the Override context manager.
from scottzach1.pif import providers
from scottzach1.pif import wiring
StringProvider = providers.ExistingSingleton("hello world")
@wiring.inject
def my_function(a: str = StringProvider):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
OverrideProvider = providers.ExistingSingleton("overridden_1")
with StringProvider.override(OverrideProvider):
assert "overridden_1" == my_function()
with OverrideProvider.override_existing("overridden_2"):
assert "overridden_2" == my_function() # You can even stack overrides!!
assert "overridden_1" == my_function()
assert "hello world" == my_function()
Examples
If you would like to see more examples, feel free to check out examples/.
Contributing
-
Clone the repository and setup with uv 🪄
git clone git@github.com:scottzach1/Python-Injection-Framework.git cd Python-Injection-Framework uv sync --dev
-
Configure pre-commit hooks 🪝
pre-commit install -
Write your changes! 💻️
-
Run test cases 🧪
pytest
-
Submit a Pull Request ↖️
Authors
| Zac Scott (scottzach1) |
Metadata
Release files for python-injection-framework 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_injection_framework-1.0.0.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_injection_framework-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.0 kB
Release files / python_injection_framework-1.0.0.tar.gz
| Download URL | python_injection_framework-1.0.0.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a21acf707a9aa3ec21f7ec9af55b2f9b70e53bd5bfe8bc8e4f86df582c9b8f82
|
|
BLAKE2b-256 checksum How to use checksums |
ed5b637787e6e2753eac31a99604da0c20cc42bb704a33d243e3edf0d5bdc6d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 15, 2025.
Transparency logRelease files / python_injection_framework-1.0.0-py3-none-any.whl
| Download URL | python_injection_framework-1.0.0-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
833b989eb66947619c69b06f714859ee00df0fd924f2c37a5865a5b395409ae2
|
|
BLAKE2b-256 checksum How to use checksums |
c721f51482d35524a83aa2a2f476d3644e6c480451e08267b5a8c9d57eca054c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 15, 2025.
Transparency log