A python dependency injection system
Project description
PyDepends
A lightweight Python dependency injection library that simplifies managing dependencies in both synchronous and asynchronous code, using decorators in a FastAPI-like style.
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
Define dependencies in a simple way.
from asyncio import run
from pydepends import inject, Depends, Provider
async def dependency() -> int:
return 5
provider = Provider()
@inject(provider)
async def main(x: int, y: int, z: int = Depends(dependency)) -> int:
return x + y + z
assert run(main(1, 2)) == 8
Here’s a simple example showing how to define dependencies as a dependency tree:
from pydepends 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 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 = Depends(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
Dependswraps 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, Version 2.0 — see the LICENSE file for details.
© 2025 Eric Hermosis. All rights reserved.
Contributing
Contributions are welcome! Please open issues or pull requests for bugs, features, or enhancements.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydepends-0.1.4.tar.gz.
File metadata
- Download URL: pydepends-0.1.4.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.1 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c433a080951da74e3a49a1c9eb44cb7c76a269ad3991f5128b414502e3fab79f
|
|
| MD5 |
4f00609e4658ceb8d66554700fe9cce6
|
|
| BLAKE2b-256 |
4f9b8fb1ab1c11613e9f06b043f94fa2383f5f5c3666504113cf635c76a6b35e
|
File details
Details for the file pydepends-0.1.4-py3-none-any.whl.
File metadata
- Download URL: pydepends-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.1 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c0c13d9cc727079de2aaa6a46b75b9b848f7063bb5c5f715d05c5a2a69a747a
|
|
| MD5 |
74ff663601799c0f5f74651121af3c7a
|
|
| BLAKE2b-256 |
765bc91add007b30b09d699b50f81a6977e9f0ac72c9c18a00e729591ea38ec8
|