Skip to main content

Another Python Dependency injector framework.

Project description

Python Injection Framework (PIF)

python version coverage pytest ruff audit

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

  1. Clone the repository and setup with uv 🪄

    git clone git@github.com:scottzach1/Python-Injection-Framework.git
    cd Python-Injection-Framework
    uv sync --dev
    
  2. Configure pre-commit hooks 🪝

    pre-commit install
    
  3. Write your changes! 💻️

  4. Run test cases 🧪

    pytest
    
  5. Submit a Pull Request ↖️

Authors

Zac Scott
Zac Scott (scottzach1)

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

python_injection_framework-1.0.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

python_injection_framework-1.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file python_injection_framework-1.0.0.tar.gz.

File metadata

File hashes

Hashes for python_injection_framework-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a21acf707a9aa3ec21f7ec9af55b2f9b70e53bd5bfe8bc8e4f86df582c9b8f82
MD5 872e2962abc0f83059ba9e5e5af89f1e
BLAKE2b-256 ed5b637787e6e2753eac31a99604da0c20cc42bb704a33d243e3edf0d5bdc6d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_injection_framework-1.0.0.tar.gz:

Publisher: pipeline.yml on scottzach1/Python-Injection-Framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_injection_framework-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_injection_framework-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 833b989eb66947619c69b06f714859ee00df0fd924f2c37a5865a5b395409ae2
MD5 ccb1f688d6d6f3b7ead980523cde0b5e
BLAKE2b-256 c721f51482d35524a83aa2a2f476d3644e6c480451e08267b5a8c9d57eca054c

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_injection_framework-1.0.0-py3-none-any.whl:

Publisher: pipeline.yml on scottzach1/Python-Injection-Framework

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