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
You can also use Annotated:
@inject(provider)
async def main(x: int, y: int, z: Annotated[int, Depends(dependency)]) -> int:
return x + y + z
Define dependencies as a deep trees:
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 M. Cardozo (Eric Hermosis). All rights reserved.
Contributing
Contributions are welcome! Please open issues or pull requests for bugs, features, or enhancements.
Metadata
Release files for pydepends 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydepends-0.1.7.tar.gz | 7.5 kB | Without provenance | Details for pydepends-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydepends-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for pydepends-0.1.7-py3-none-any.whl |
Total release size: 16.2 kB
Release files / pydepends-0.1.7.tar.gz
| Download URL | pydepends-0.1.7.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0bca6a4a010c53ff22be2ccf450cd0bbf412e4f66497223dc95ac6a7ec9f35c
|
|
BLAKE2b-256 checksum How to use checksums |
e075d612df5b2b6b5125d5929db6b0dce625b42bcc1f5fb4f20ed7cd8e97ad95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.1 Linux/6.17.0-1020-azure
|
Release files / pydepends-0.1.7-py3-none-any.whl
| Download URL | pydepends-0.1.7-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a413469010076e8f4064293ab4d946e435fed327168c66f4aa8e340d74f571c4
|
|
BLAKE2b-256 checksum How to use checksums |
d3ba72e401a8ddc07a40161165e611ae2b75fd0fcfff0b963e720f5834a7f9c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.1 Linux/6.17.0-1020-azure
|
Security for pydepends 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.