Skip to main content

Python Dependency Injection Library

Project description

WireUp

GitHub GitHub Workflow Status (with event) Code Climate maintainability PyPI - Python Version PyPI - Version

Dependency injection library designed to provide a powerful and flexible way to manage and inject dependencies making it easier to develop, test, and maintain Python codebases.


Quickstart guide

Demonstration of a simple application with two services (classes that provide functionality) and parameters (app configuration) showing automatic dependency resolution and injection (autowiring) for services and a simple web view called "greet".

1. Register dependencies

from wireup import container, Wire

# Parameters serve as configuration for services. 
# Populate this with your existing configuration
# Think of a database url or environment name.
container.params.update(...)


# Register a class as a dependency in the container.
# Constructor injection is supported for regular classes as well as dataclasses.
# In turn this can also depend on other objects in the container.
@container.register
class DbService:
    # Inject a parameter by name
    connection_str: Annotated[str, Wire(param="db.connection_str")],
    # Or by interpolating multiple parameters into a string
    cache_dir: Annotated[str, Wire(expr="${cache_dir}/${env}/db")],


@container.register
@dataclass
class UserRepository:
    db: DbService  # Services may also depend on other dependencies.

2. Inject

@app.route("/greet/<str:name>")
@container.autowire
# Decorate all targets where the library must perform injection,such as views in a web app.
# Classes are automatically injected based on annotated type. 
# Parameters will be located based on their annotation metadata.
def greet(
    name: str, 
    user_repository: UserRepository,  
    env: Annotated[str, Wire(param="env")]
): 
    ...

Installation

# Install using poetry:
poetry add wireup

# Install using pip:
pip install wireup

Documentation

For more information check out the documentation

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

wireup-0.4.0.tar.gz (12.6 kB view hashes)

Uploaded Source

Built Distribution

wireup-0.4.0-py3-none-any.whl (15.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page