---
title: "fast-depends | pypx"
description: "FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported."
canonical_url: "https://pypx.app/packages/fast-depends"
meta:
  "og:description": "FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_fast-depends%2Cversion_3.0.11%2Csummary_FastDepends%2B-%2Bextracted%2Band%2Bcleared%2Bfrom%2BHTTP%2Bdomain%2Blogic%2BFastAPI%2BDependency%2BInjection%2BSystem.%2BAsync%2Band%2Bsync%2Bare%2Bboth%2Bsupported.%2Clicense_MIT%2Cp_Ii9wYWNrYWdlcy9mYXN0LWRlcGVuZHMi%2Cs_0865doL2vlLXZLmh.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "fast-depends 3.0.11"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_fast-depends%2Cversion_3.0.11%2Csummary_FastDepends%2B-%2Bextracted%2Band%2Bcleared%2Bfrom%2BHTTP%2Bdomain%2Blogic%2BFastAPI%2BDependency%2BInjection%2BSystem.%2BAsync%2Band%2Bsync%2Bare%2Bboth%2Bsupported.%2Clicense_MIT%2Cp_Ii9wYWNrYWdlcy9mYXN0LWRlcGVuZHMi%2Cs_0865doL2vlLXZLmh.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **fast-depends**

v3.0.11

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

25 KB Python 3.10+ py3-none-anyMIT2 deps

[**Diff **](https://pypx.app/packages/fast-depends/diff)

`$ uv add fast-depends`

# 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*](https://fastapi.tiangolo.com/) and [*pydantic*](https://docs.pydantic.dev/) 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**](https://github.com/Lancetnik/Propan):rocket: (and [**FastStream**](https://github.com/airtai/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
```

## **Details **

<dl>

<dt>Version</dt>
<dd>3.0.11</dd>

<dt>License</dt>
<dd>MIT</dd>

<dt>Python</dt>
<dd>&gt;=3.10</dd>

<dt>Maintainer</dt>
<dd>Nikita Pastukhov</dd></dl>

## **Links**

- [Documentation](https://lancetnik.github.io/FastDepends/)
- [Homepage](https://lancetnik.github.io/FastDepends/)
- [Source](https://github.com/Lancetnik/FastDepends)
- [Tracker](https://github.com/Lancetnik/FastDepends/issues)
- [View on PyPI](https://pypi.org/project/fast-depends/)

## **Release Cadence **

**10**

releases in the past year

avg 16 days between releases

## **Maintainers **

- Nikita Pastukhov · [nikita@pastukhov-dev.ru](mailto:nikita@pastukhov-dev.ru)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/fast-depends#webpage","@type":"WebPage","description":"FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported.","name":"fast-depends","url":"https://pypx.app/packages/fast-depends","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/fast-depends"]}]},{"@id":"https://pypx.app/packages/fast-depends#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"FastDepends - extracted and cleared from HTTP domain logic FastAPI Dependency Injection System. Async and sync are both supported.","license":"MIT","name":"fast-depends","softwareVersion":"3.0.11"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
