FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported.
Project description
FastDepends
Documentation: https://lancetnik.github.io/FastDepends/
FastDepends - FastAPI Dependency Injection system extracted from FastAPI and cleared of all HTTP logic. This is a small library which provides you with the ability to use lovely FastAPI interfaces in your own projects or tools.
Thanks to fastapi and pydantic projects for this greate functionality. This package is just a small change of the original FastAPI sources to provide DI functionality in a pyre-Python way.
Async and sync modes are both supported.
For why?
This project should be extreamly helpfull to boost your not-FastAPI applications (even Flask, I know that u like some legacy).
Also the project can be a core of your own framework for anything. Actually, it was build for my another project - :rocket:Propan:rocket:, check it to see full-featured FastDepends usage example.
Installation
pip install fast-depends
Usage
There is no way to make Dependency Injection easier
You can use this library without any frameworks in both sync and async code.
Async code
import asyncio
from fast_depends import inject, Depends
async def dependency(a: int) -> int:
return a
@inject
async def main(
a: int,
b: int,
c: int = Depends(dependency)
) -> float:
return a + b + c
assert asyncio.run(main("1", 2)) == 4.0
Sync code
from fast_depends import inject, Depends
def dependency(a: int) -> int:
return a
@inject
def main(
a: int,
b: int,
c: int = Depends(dependency)
) -> float:
return a + b + c
assert main("1", 2) == 4.0
@inject
decorator plays multiple roles at the same time:
- resolve Depends classes
- cast types according to Python annotation
- validate incoming parameters using pydantic
Features
Synchronous code is fully supported in this package: without any async_to_sync
, run_sync
, syncify
or any other tricks.
Also, FastDepends casts functions' return values the same way, it can be very helpful in building your own tools.
These are two main defferences from native FastAPI DI System.
Custom Fields
If you wish to write your own FastAPI or another closely by architecture tool, you should define your own custom fields to specify application behavior.
Custom fields can be used to adding something specific to a function arguments (like a BackgroundTask) or parsing incoming objects special way. You able decide by own, why and how you will use these tools.
FastDepends grants you this opportunity a very intuitive and comfortable way.
from fast_depends import inject
from fast_depends.library import CustomField
class Header(CustomField):
def use(self, **kwargs: AnyDict) -> AnyDict:
kwargs = super().use(**kwargs)
kwargs[self.param_name] = kwargs["headers"][self.param_name]
return kwargs
@inject
def my_func(header_field: int = Header()):
return header_field
assert my_func(
headers={ "header_field": "1" }
) == 1
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fast_depends-2.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0428030b3e1c449bcd23b6a05b680528efb7876ead3bc87163f98773ff8e15a6 |
|
MD5 | 5fce4080dfe296e3201729864f4b6d40 |
|
BLAKE2b-256 | 700121e79db32c9c126c04d26e1ec807e8ce35907131f2b7761580a41e89dfbc |