Skip to main content

FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported.

Project description

FastDepends

Tests coverage Coverage Package version downloads
Supported Python versions GitHub


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 great functionality. This package is just a small change of the original FastAPI sources to provide DI functionality in a pure-Python way.

Async and sync modes are both supported.

For why?

This project should be extremely helpful 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: (and FastStream), 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.


Dependencies Overriding

Also, FastDepends can be used as a lightweight DI container. Using it, you can easily override basic dependencies with application startup or in tests.

from typing import Annotated

from fast_depends import Depends, dependency_provider, inject

def abc_func() -> int:
    raise NotImplementedError()

def real_func() -> int:
    return 1

@inject
def func(
    dependency: Annotated[int, Depends(abc_func)]
) -> int:
    return dependency

with dependency_provider.scope(abc_func, real_func):
    assert func() == 1

dependency_provider in this case is just a default container already declared in the library. But you can use your own the same way:

from typing import Annotated

from fast_depends import Depends, Provider, inject

provider = Provider()

def abc_func() -> int:
    raise NotImplementedError()

def real_func() -> int:
    return 1

@inject(dependency_overrides_provider=provider)
def func(
    dependency: Annotated[int, Depends(abc_func)]
) -> int:
    return dependency

with provider.scope(abc_func, real_func):
    assert func() == 1

This way you can inherit the basic Provider class and define any extra logic you want!


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

fast_depends-3.0.2.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fast_depends-3.0.2-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file fast_depends-3.0.2.tar.gz.

File metadata

  • Download URL: fast_depends-3.0.2.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_depends-3.0.2.tar.gz
Algorithm Hash digest
SHA256 733269b1a33fa006759c840c64a213b590e0a888a9c388472fdde909668233e5
MD5 ebbd85e1b17bf37d0c1de3108794df3e
BLAKE2b-256 a9e93a7b647f5cdee6cbde3365eb480d5074e9352ca9b7af7e997516ff1a08f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_depends-3.0.2.tar.gz:

Publisher: publish_pypi.yml on Lancetnik/FastDepends

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_depends-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: fast_depends-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_depends-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aff1309e70e07995c8a208e8c4dfa90e9a6edb44e0f3a35d5d927e8917c47aa1
MD5 24ff96be222f8536081766ec87fa3c49
BLAKE2b-256 4e9929a0d40aa6d03a49f34f43f36df1ce096e41fc44f858f83b1aaf50913930

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_depends-3.0.2-py3-none-any.whl:

Publisher: publish_pypi.yml on Lancetnik/FastDepends

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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