Skip to main content

Backports for FastAPI

Project description

FastAPI Backports

License: MIT Python 3.8+ FastAPI 0.115.0+

A Python library that provides backports for FastAPI features, allowing you to use newer FastAPI functionality on older versions. This includes both unreleased fixes and experimental features based on FastAPI community discussions that might be included in future FastAPI releases.

Features

This library includes backports for:

🔄 Multiple Query Models Support

  • Issue: Support multiple Query() in a single endpoint
  • Description: Enables using multiple Pydantic models as query parameters in a single FastAPI endpoint
  • Benefits: Better organization of query parameters and improved API design

What this fixes:

import fastapi_backports.apply  # noqa: F401

from dataclasses import dataclass
from fastapi import Query, FastAPI
from typing import Annotated, Any


@dataclass
class FilterParams:
    category: str | None = None
    min_price: float | None = None


@dataclass
class PaginationParams:
    page: int = 1
    size: int = 10


app = FastAPI()


# ❌ Without backport: This would not work as expected
@app.get("/items")
async def get_items(
    filters: Annotated[FilterParams, Query()],
    pagination: Annotated[PaginationParams, Query()],
) -> dict[str, Any]:
    return {"filters": filters, "pagination": pagination}

# ✅ With backport: This works perfectly!
# Query parameters are properly flattened and validated
# Example: /items?category=electronics&min_price=10&page=2&size=20

🏷️ PEP 695 Type Alias Support

What this fixes:

import fastapi_backports.apply  # noqa: F401

from typing import Annotated
from fastapi import Depends, FastAPI


async def get_current_user_id() -> int:
    return 123


# PEP 695 style type alias (Python 3.12+)
type UserId = Annotated[int, Depends(get_current_user_id)]

app = FastAPI()


# ❌ Without backport: FastAPI doesn't recognize the type alias properly
# ✅ With backport: Type aliases work seamlessly in dependencies
@app.get("/users/{user_id}")
async def get_user(user_id: UserId):
    return {"user_id": user_id}

🕵️ Add middleware parameter to APIRouter

  • Issue: Add middleware parameter to APIRouter
  • Description: Enables adding middleware directly to APIRouter instances and individual routes
  • Benefits: Better middleware organization and route-specific middleware support

What this fixes:

import fastapi_backports.apply  # noqa: F401

from typing import Any

from fastapi_backports import FastAPI, APIRouter
from fastapi import Request
from fastapi.middleware import Middleware
from starlette.types import ASGIApp, Receive, Scope, Send


def add_header_middleware(_app: ASGIApp, header_name: str, header_value: str) -> ASGIApp:
    async def _middleware(scope: Scope, receive: Receive, send: Send) -> None:
        async def send_wrapper(message):
            if message["type"] == "http.response.start":
                headers = message.setdefault("headers", [])
                headers.append((header_name.encode(), header_value.encode()))
            await send(message)

        await _app(scope, receive, send_wrapper)

    return _middleware


app = FastAPI()

# ❌ Without backport: APIRouter doesn't accept middleware parameter
# ✅ With backport: You can now add middleware to routers
router = APIRouter(
    prefix="/api/v1",
    middleware=[
        Middleware(add_header_middleware, header_name="X-Custom-Header", header_value="Value")
    ]
)


@router.get("/items")
async def get_items() -> dict[str, Any]:
    return {"items": ["item1", "item2"]}


# Middleware will be applied to all routes in this router
app.include_router(router)

🔍 QUERY HTTP method support

What this fixes:

import fastapi_backports.apply  # noqa: F401

from typing import Any
from fastapi import FastAPI
from pydantic import BaseModel

class SearchQuery(BaseModel):
    filters: dict[str, Any]
    sort_by: str | None = None
    limit: int = 100

app = FastAPI()

# ❌ Without backport: QUERY method not supported
# ✅ With backport: You can now use the QUERY HTTP method
@app.query("/search")
async def search_items(query: SearchQuery) -> dict[str, Any]:
    return {
        "results": ["item1", "item2", "item3"],
        "filters_applied": query.filters,
        "sorted_by": query.sort_by,
        "total": 150
    }

# QUERY method allows complex search parameters in request body
# while maintaining safe, idempotent semantics
# Usage: QUERY /search with JSON body containing SearchQuery data

📝 Postponed Type Annotations Support

  • Issue: Can't use Annotated with ForwardRef
  • Description: Fixes handling of postponed type annotations (PEP 563) and forward references in FastAPI
  • Benefits: Enables proper type checking and dependency injection with complex type hierarchies

What this fixes:

from __future__ import annotations

import fastapi_backports.apply  # noqa: F401

from dataclasses import dataclass
from typing import Annotated

from fastapi import Depends, FastAPI

app = FastAPI()


# Forward reference to a class defined later
def get_potato() -> Potato:
    return Potato(color='red', size=10)


# ❌ Without backport: ForwardRef causes issues with dependency injection
# ✅ With backport: Forward references work seamlessly
@app.get('/')
async def read_root(potato: Annotated[Potato, Depends(get_potato)]) -> Potato:
    return potato


@dataclass
class Potato:
    color: str
    size: int

🔄 Multiple Lifespans Support

  • Issue: Support multiple Lifespan in FastAPI app
  • Description: Enables adding multiple lifespan context managers to FastAPI applications and routers
  • Benefits: Better organization of startup/shutdown logic and modular lifespan management

What this fixes:

from contextlib import asynccontextmanager
from typing import AsyncIterator, Any

import fastapi_backports.apply  # noqa: F401

from fastapi import FastAPI

app = FastAPI()

# ❌ Without backport: Only one lifespan context is supported
# ✅ With backport: You can add multiple lifespan contexts

@app.add_lifespan
@asynccontextmanager
async def database_lifespan(_app: FastAPI) -> AsyncIterator[dict[str, Any]]:
    print("Starting database connection")
    yield {"db": "connection"}
    print("Closing database connection")


@app.add_lifespan
@asynccontextmanager
async def cache_lifespan(_app: FastAPI) -> AsyncIterator[dict[str, Any]]:
    print("Starting cache")
    yield {"cache": "connection"}
    print("Stopping cache")


@app.get("/")
async def read_root():
    return {"message": "Hello World"}

# Both lifespans will be executed in order during startup/shutdown

Installation

pip install fastapi-backports

Usage

Basic Usage

Import the backport module before using FastAPI:

import fastapi_backports.apply  # noqa: F401
from fastapi import FastAPI, APIRouter

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

The backports are automatically applied when you import fastapi_backports.apply.

💡 Note: The # noqa: F401 comment is needed to prevent linters from complaining about an "unused import". While the import appears unused, it actually applies the backports through side effects when imported.

Manual Backport Control

For more control over when backports are applied:

import fastapi_backports
from fastapi import FastAPI

# ⚠️ IMPORTANT: Apply backports BEFORE creating FastAPI app or routes
fastapi_backports.backport()

# Now create your app
app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

⚠️ Important: Always call fastapi_backports.backport() before creating your FastAPI application instance or defining any routes. The backports modify FastAPI's internal behavior and must be applied before FastAPI processes your route definitions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_backports-0.1.3.tar.gz (85.6 kB view details)

Uploaded Source

Built Distribution

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

fastapi_backports-0.1.3-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_backports-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi_backports-0.1.3.tar.gz
  • Upload date:
  • Size: 85.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.24

File hashes

Hashes for fastapi_backports-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e64a6f75ef4a798808691902fcbdba2f0fa5c6edabde720bd5dd0644e8e7b147
MD5 6d4486b545fb6247633e5aba5b574b96
BLAKE2b-256 102dbe21f24ecbbc6665143061a6580175503a6ca8b7b9a242688117631c8d27

See more details on using hashes here.

File details

Details for the file fastapi_backports-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_backports-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 59cf768ca6d0fa9b02484f0e2841ab5b510575bc3a18350ffb68e3568c1a6102
MD5 2d8dcef3fda151400cd29c7f6a6dffe5
BLAKE2b-256 46d63f2e84ff30fd5533eead4ce5c1d3cfc4efe9d4eaefc6e1650b853dd56e48

See more details on using hashes here.

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