Skip to main content

python_depends

python_depends is a dependency injection library inspired by the Depends mechanism from FastAPI. It supports both asynchronous and synchronous functions, providing flexibility and ease of use.

Installation

pip install depends_python

Key Features

  • Support for synchronous and asynchronous functions.
  • Simple dependency annotation using the @inject_dependencies decorator.
  • Support for nested dependencies.
  • Ability to mix synchronous and asynchronous dependencies.

Usage Examples

Asynchronous Functions

from python_depends import Depends, inject_dependencies
import asyncio

async def injected():
    return 1

@inject_dependencies
async def inject_value(value: int = Depends(injected)):
    print(value)

if __name__ == '__main__':
    asyncio.run(inject_value())

Basic Example

from python_depends import Depends, inject_dependencies

# Function to inject
def get_value():
    return 1

@inject_dependencies  # inject dependencies
def test_injection(value: int = Depends(get_value)):
    print(value)

if __name__ == '__main__':
    test_injection()

Multiple Dependencies

from python_depends import inject_dependencies, Depends

def injected1():
    return 1

def injected2():
    return 3

@inject_dependencies
def inject_value(value1: int = Depends(injected2), value2: int = Depends(injected1)):
    print(value1 + value2)

if __name__ == "__main__":
    inject_value()

Nested Dependencies

from python_depends import Depends, inject_dependencies

def injected1():
    return 1

@inject_dependencies
def injected2(value: int = Depends(injected1)):
    new_value = 3
    return new_value + value

@inject_dependencies
def inject_value(value: int = Depends(injected2)):
    print(value)

if __name__ == '__main__':
    inject_value()

Mixing Synchronous and Asynchronous Dependencies

from python_depends import Depends, inject_dependencies

async def injected():
    return 1

@inject_dependencies
def inject_value(value: int = Depends(injected)):
    print(value)

if __name__ == '__main__':
    inject_value()

License

MIT License.


The python_depends library is designed to simplify the dependency injection process and make code cleaner and easier to test.

The name of package is depends_python because python_depends is already in use

Metadata

Release files for depends_python 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for depends_python 0.0.1
File Size Uploaded
depends_python-0.0.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for depends_python 0.0.1
File Interpreter ABI Platform
depends_python-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / depends_python-0.0.1.tar.gz

Download URL depends_python-0.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
670dfed9a28b8aa6da8059d3c207fd4843417559f9965f4909ec2a356015efe9
BLAKE2b-256 checksum
How to use checksums
066b5112c7de1a5285286a8e2df5c8706148f60a7bea573b46b02e71619c7d11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / depends_python-0.0.1-py3-none-any.whl

Download URL depends_python-0.0.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
637018d1ff661d647b9b79940e69c20df1b294a30a7ba1d9f7d5e8869d254f20
BLAKE2b-256 checksum
How to use checksums
ec6158914009a83b717566d0244f40ada1943d6f650ccbcea63d919ad1c4f944
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page