Skip to main content

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.

License

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

  • 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, 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

pydepends-0.1.5.tar.gz (7.5 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.5-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydepends-0.1.5.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.1 Linux/6.11.0-1018-azure

File hashes

Hashes for pydepends-0.1.5.tar.gz
Algorithm Hash digest
SHA256 50c3bc9baba5f34d2b52da1d0e14d20f53d6871d409d15b42e301209fb84ddb1
MD5 6ca81d7caa9f762805a789dd9fb2bfe1
BLAKE2b-256 9c724ec7b5fe5befbe0ecfcee81ae2f56ca031c3d37e4ef1602576aa3ebd4a21

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pydepends-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9ad3278078375e5bc1ee453ca8dfee64d78dd77033c833075a6359dd353005fb
MD5 7a8484884f7dc09ac313801d4f976c16
BLAKE2b-256 75e0730bdc2a43552b353c053801027bca91f516b3e4081623f19cb3dbe92982

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