
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
python-injection-framework
Advanced tools
A simple Python dependency injection framework.
You can install this project from pypi.
pip install python-injection-framework
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
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()
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()
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.
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"
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()
If you would like to see more examples, feel free to check out examples/.
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 ↖️
Zac Scott (scottzach1) |
FAQs
Another Python Dependency injector framework.
We found that python-injection-framework demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.