Skip to main content

Tests Linters Documentation PyPI package

Source code: https://github.com/volfpeter/fasted

Documentation and examples: https://volfpeter.github.io/fasted

FastED

FastAPI dependencies and utilities.

Installation

The package is available on PyPI and can be installed with:

$ pip install fasted

Features

selfdependent

Decorator that let's you use your business objects' instance methods as FastAPI dependencies without writing any additional code.

Supports:

  • Sync and async instance methods.
  • Sync and async generator methods.
  • Inheritence and super() calls is decorated methods.
  • An optional factory (FastAPI dependency) for creating the self instance. If not set, the class' __init__() method will serve as the dependency for creating the self instance.
  • Decorated instance methods will behave as expected if called directly.

Example use:

from typing import Annotated

from fastapi import Depends, FastAPI
from fasted import selfdependent


def double() -> "Multiplier":
    # Dependency that returns a Multiplier with base = 2.
    return Multiplier(2)


class Multiplier:
    def __init__(self, base: float) -> None:
        self.base = base

    @selfdependent()
    def multiply(self, mul: float) -> float:
        # `__init__()` will be used as the dependency to create `self`, so the route
        # where this method is used will have a `base` and a `mul` query parameter.
        return self.base * mul

    @selfdependent(factory=double)
    async def double(self, mul: float) -> float:
        # `double()` will be used as the dependency to create `self`, so the route
        # where this method is used will only have a `mul` query parameter.
        return self.base * mul


app = FastAPI()


@app.get("/multiply")
def multiply_route(value: Annotated[float, Depends(Multiplier.multiply)]) -> float:
    # FastAPI will create the `Multiplier` instance based on `Multiplier.__init__()` and
    # automatically feed this `instance` as `self` to `Multiplier.multiply()` to calculate
    # the value of the dependency.
    return value


@app.get("/double")
def double_route(value: Annotated[float, Depends(Multiplier.double)]) -> float:
    # FastAPI will create the `Multiplier` instance using the `double()` factory (dependency)
    # and automatically feed this instance as `self` to `Multiplier.multiply()` to
    # calculate the value of the dependency.
    return value

Dependency

Generic type for FastAPI dependencies.

Example use:

from typing import Annotated, Generator

from fastapi import FastAPI, APIRouter
from fasted import Dependency
# from x import Session


def make_api(make_session: Dependency[Session]) -> APIRouter:
    DependsSession = Annotated[Session, Depends(make_session)]

    api = APIRouter()

    @api.get("/")
    def get(session: DependsSession) -> int:
        return 4

    return api


def make_db_session() -> Generator[Session, None, None]:
    with Session(database) as session:
        yield session


app = FastAPI()
app.include_router(make_api(make_db_session), prefix="/random-number")

Dependencies

Being a FastAPI utility library, the only dependency is (and will remain) fastapi.

Development

Use ruff for linting and formatting, mypy for static code analysis, and pytest for testing.

The documentation is built with mkdocs-material and mkdocstrings.

Contributing

All contributions are welcome.

License - MIT

The package is open-sourced under the conditions of the MIT license.

Metadata

Release files for fasted 0.2404.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fasted 0.2404.2
File Size Uploaded
fasted-0.2404.2.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fasted 0.2404.2
File Interpreter ABI Platform
fasted-0.2404.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / fasted-0.2404.2.tar.gz

Download URL fasted-0.2404.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f08145ac89edd1a7ac25f1e8a76ae260c524959a5195c23509a103c3a92353fb
BLAKE2b-256 checksum
How to use checksums
2c3b85486aeff22948f2439483f080843e4431a40100172d8fabcb5cb0ce0b22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / fasted-0.2404.2-py3-none-any.whl

Download URL fasted-0.2404.2-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4f59dbc1393817559c5fa0c0e226e72178c1eccb8b0e2c2a143d4d47cac19cb
BLAKE2b-256 checksum
How to use checksums
a68e60b17fe1ea4c1c95c98042adadf88035ecb1a86d60c8adedafa3edea161a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2404.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page