Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maksimzayats/fastapi-better-di
A utility that allows you to use DI in fastapi without Depends()
https://github.com/maksimzayats/fastapi-better-di
dependency-injection di fastapi fastapi-dependency fastapi-dependency-injection fastapi-di fastapidi
Last synced: 3 months ago
JSON representation
A utility that allows you to use DI in fastapi without Depends()
- Host: GitHub
- URL: https://github.com/maksimzayats/fastapi-better-di
- Owner: MaksimZayats
- Archived: true
- Created: 2022-04-15T19:01:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-20T14:08:59.000Z (almost 2 years ago)
- Last Synced: 2024-09-28T02:21:50.311Z (3 months ago)
- Topics: dependency-injection, di, fastapi, fastapi-dependency, fastapi-dependency-injection, fastapi-di, fastapidi
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fastapi-better-di
## What is this ?
`fastapi-better-di` is a utility that allows you to use DI in fastapi without Depends()## Installation
```shell
pip install fastapi_better_di
```## Examples
```python
# app.py
import uvicorn
from fastapi import FastAPI
from fastapi_better_di.patcher.auto import is_pathed
# functions were patched immediately after importassert is_pathed(), "Something went wrong"
class MyType:
def __init__(self, value):
self.value = valueapp = 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_typeif __name__ == "__main__":
uvicorn.run(app)
```[See all examples](examples)
## Usage
1. Patching:
* Auto patching: patches classes when importing:
```python
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:
```python
from fastapi_better_di.patcher.manual import patch, is_pathedpatch()
# To check if everything is OK, use assert
assert is_pathed(), "Something went wrong"
```* [Examples](examples)
* **IMPORTANT**: You can still use `= Depends()` without a function as an argument,
and it won't add unnecessary arguments to the swagger.
* Related issue: [fastapi issue](https://github.com/tiangolo/fastapi/issues/4118)* **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