Skip to main content

fastapi-better-di

What is this ?

fastapi-better-di is a utility that allows you to use DI in fastapi without Depends()

Installation

pip install fastapi_better_di

Examples

# app.py
import uvicorn
from fastapi import FastAPI
from fastapi_better_di.patcher.auto import is_pathed
# functions were patched immediately after import

assert is_pathed(), "Something went wrong"


class MyType:
    def __init__(self, value):
        self.value = value


app = FastAPI()
app.dependency_overrides[MyType] = lambda: MyType(123)


@app.get("/")
def handler(my_type: MyType):  # <- DI without `Depends()`
    assert my_type.value == 123
    return my_type


if __name__ == "__main__":
    uvicorn.run(app)

See all examples

Usage

  1. Patching:
    • Auto patching: patches classes when importing:

      from fastapi_better_di.patcher.auto import is_pathed # The classes were patched immediately after import
      
      # To check if everything is OK, use assert
      assert is_pathed(), "Something went wrong"
      
    • Manual patching: you need to call patch() by yourself:

      from fastapi_better_di.patcher.manual import patch, is_pathed
      
      patch()
      
      # To check if everything is OK, use assert
      assert is_pathed(), "Something went wrong"
      
    • Examples

  • IMPORTANT: You can still use = Depends() without a function as an argument, and it won't add unnecessary arguments to the swagger.

  • IMPORTANT: The main app(FastAPI) and dependency_overrides must be initialized before importing routers!

How it works

fastapi-better-di simply patch the handler function and add = Depends(func) as the default argument

Metadata

Release files for fastapi-better-di 0.3.4

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

Source distribution (sdist)

Source distribution for fastapi-better-di 0.3.4
File Size Uploaded Provenance
fastapi-better-di-0.3.4.tar.gz 3.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-better-di 0.3.4
File Interpreter ABI Platform
fastapi_better_di-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / fastapi-better-di-0.3.4.tar.gz

Download URL fastapi-better-di-0.3.4.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b470ba3502f50ef4eadcfa3e30af65bdae593bbb1d9f7d9f9ba5ac02c612cefc
BLAKE2b-256 checksum
How to use checksums
02871610cc96360e3fae47f84da24d02fdede0f4347befbb43edb5f05591b982
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / fastapi_better_di-0.3.4-py3-none-any.whl

Download URL fastapi_better_di-0.3.4-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb1974dc3bbced8942b6c71b3ba74100faab3ebc6c723f1944a54c93bb900bb4
BLAKE2b-256 checksum
How to use checksums
b048b919e7da4bc7d5f389a0d34dee63868a0bd4be60478334b65a0b4d94183e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Security for fastapi-better-di 0.3.4

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.

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