Skip to main content

No project description provided

Project description

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

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

fastapi-better-di-0.3.4.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

fastapi_better_di-0.3.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-better-di-0.3.4.tar.gz.

File metadata

  • Download URL: fastapi-better-di-0.3.4.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for fastapi-better-di-0.3.4.tar.gz
Algorithm Hash digest
SHA256 b470ba3502f50ef4eadcfa3e30af65bdae593bbb1d9f7d9f9ba5ac02c612cefc
MD5 89dd315f7d5d573e731843bf876906eb
BLAKE2b-256 02871610cc96360e3fae47f84da24d02fdede0f4347befbb43edb5f05591b982

See more details on using hashes here.

File details

Details for the file fastapi_better_di-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_better_di-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cb1974dc3bbced8942b6c71b3ba74100faab3ebc6c723f1944a54c93bb900bb4
MD5 6aadb7bf4fc11cfe442d6f6acf64ebec
BLAKE2b-256 b048b919e7da4bc7d5f389a0d34dee63868a0bd4be60478334b65a0b4d94183e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page