Skip to main content

Dependency injection library for python

Project description

PyDepends

A lightweight Python dependency injection library designed to simplify managing dependencies in synchronous and asynchronous code using decorators and type-safe wrappers.


Features

  • Dependency injection with support for synchronous and asynchronous dependencies.
  • Dependency overrides via a provider for easy testing and configuration.
  • Context-managed dependency lifecycles with support for generators and async generators.
  • Simple API with decorators and wrappers for clean, maintainable code.

Installation

pip install pydepends

Quickstart

Here’s a simple example showing how to define dependencies as a dependency tree:

from pydepends.depends import inject, Depends 
 
def left_leaf_dependency():
    return 2

async def right_leaf_dependency():
    yield 3 #generators supported
 
async def right_node_dependency(leaf = Depends(right_leaf_dependency)):
    return leaf * 5
 
async def root_dependency(left: int = Depends(left_leaf_dependency), right: int = Depends(right_node_dependency)) -> int:
    return left * right * 7

Inject the dependencies in a sync function:

from pydepends.depends import Provider
 
provider = Provider()

@inject(provider)
def handle_dependency(root: int = Depends(root_dependency)) -> int:
    return root * 11
 
value = handle_dependency()

assert value == 2 * 3 * 5 * 7 * 11
print(f"Computed value: {value}")  # Output: Computed value: 2310

Or inject them in an async function. Sync dependencies are put into an asyncio thread to avoid blocking the event loop.

@inject(provider)
async def async_handle_dependency(root: int = Dependency(root_dependency)):
    return root*11

async def main():
    value = await async_handle_dependency()
    assert value == 2*3*5*7*11 
    print(f"Computed value: {value}")  # Output: Computed value: 2310


import asyncio
asyncio.run(main())

How it works

  • Depends wraps a callable into a Dependency object.
  • Provider allows overriding dependencies (useful for testing or different environments).
  • The @inject decorator resolves and injects dependencies based on the provider.
  • Supports both synchronous and asynchronous callables, managing contexts as needed.

License

This project is licensed under the Apache License 2.0 — see the LICENSE file for details. Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page or submit a pull request.

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

pydepends-0.1.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

pydepends-0.1.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pydepends-0.1.2.tar.gz.

File metadata

  • Download URL: pydepends-0.1.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.11.0-1015-azure

File hashes

Hashes for pydepends-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f8e7b50be598edd8b84cf492fa2419a6f2a0366715af2be31ba0e23f2c752fb3
MD5 32e619d1d6f0a36ab56cef56bfcf6e1c
BLAKE2b-256 5d0c6a1ad92435157caafa456fc6c09f68c4132a0a3f41fb264af4d0134db78a

See more details on using hashes here.

File details

Details for the file pydepends-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pydepends-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.11.0-1015-azure

File hashes

Hashes for pydepends-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6d29bf9c030b243571877da42abfab956eb598dfa04ac05c7091ed1e71bb78c6
MD5 b27a3a31e8e19db2d6cb36118f4f6d74
BLAKE2b-256 07e2a7116cdd0cb5f41869f5ba1e235cbcc68e3397e31be68907bc3cb0608e65

See more details on using hashes here.

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