Skip to main content

fastapi-wraps

Python 3.10 codecov license

functools.wraps for endpoints decorators in FastAPI.

It updates the signature of the wrapped function with parameters defined in the decorator's wrapper function. All parameters of the wrapper function should have defaults assigned.

It's advised to name the parameter with some prefix, for example __, to avoid any name conflicts in decorated functions.

To use the Request object in the decorator's wrapper function use __request: Request = Depends(get_request)

Installation

pip install fastapi-wraps

Example

def save_request(
    endpoint: Callable[P, Awaitable[RT]],
) -> Callable[P, Awaitable[RT]]:
    @fastapi_wraps(endpoint)
    async def wrapper(
        *args: Any,
        __request: Request = Depends(get_request),
        __db: Db = Depends(get_db),
        **kwargs: Any,
    ) -> RT:
        __db.save(__request)
        response = await endpoint(*args, **kwargs)
        return response

    return wrapper


app = FastAPI()


@app.get("/")
@save_request
async def hello() -> str:
    return "hello"

Why?

To use dependencies provided by FastAPI's DI framework all dependencies have to be declared in the signature of the endpoint. Hence, the decorator cannot simply use functools.wraps, as functools.wraps maintains the signature of the wrapped function. The fastapi_wraps decorator takes updates the resulting signature by merging parameters from the wrapper and the wrapped function.

Release files for fastapi-wraps 0.1.4

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

Source distribution (sdist)

Source distribution for fastapi-wraps 0.1.4
File Size Uploaded
fastapi-wraps-0.1.4.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-wraps 0.1.4
File Interpreter ABI Platform
fastapi_wraps-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / fastapi-wraps-0.1.4.tar.gz

Download URL fastapi-wraps-0.1.4.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
73f4cc07caab3d3cadbed79a97c9101d41d13d8056fc87e34d58fd4d10083944
BLAKE2b-256 checksum
How to use checksums
7f22b3c3ce00bc77a4f373284910a1c3e60855c6f15acc3d40baa8417bfc354f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.10.2 Linux/5.15.0-1019-azure

Release files / fastapi_wraps-0.1.4-py3-none-any.whl

Download URL fastapi_wraps-0.1.4-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3da9e07e2a294af1b55801b963747cfc08ec88a5d1004eb85623d83a8e044bae
BLAKE2b-256 checksum
How to use checksums
2df6bdd42585e455b5a456b3a00a7b3e3b72f8f51df70d6c1d5ad2bb166a3075
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.10.2 Linux/5.15.0-1019-azure

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

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