Backports for FastAPI
Project description
FastAPI Backports
A Python library that provides backports for FastAPI features, allowing you to use newer FastAPI functionality on older versions.
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:
from dataclasses import dataclass
from fastapi_backports import FastAPI
from fastapi import Query
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
- Issue: Annotated dependencies are interpreted incorrectly when using PEP 695-style type alias
- Description: Fixes handling of modern Python type aliases (PEP 695) in FastAPI dependencies
- Benefits: Full compatibility with modern Python typing features
What this fixes:
from typing import Annotated
from fastapi_backports import FastAPI
from fastapi import Depends
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}
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: F401comment 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_backports-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_backports-0.1.0.tar.gz
- Upload date:
- Size: 54.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48640cb508ce8d2fad6d96f21e9f29f05b628542f74816ac860e7912d08bfcd9
|
|
| MD5 |
70ee7810dea709beae7ecba8f53c568a
|
|
| BLAKE2b-256 |
0558cbc1b918f093f78ef163b3bcd637538f80624a11c7ca189572c2b854f561
|
File details
Details for the file fastapi_backports-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_backports-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edb19573e880245efed93ceb7e82c45d34227bc6622f1cd67739f4417b2f26d8
|
|
| MD5 |
209e76be97484630ac18910291f921e5
|
|
| BLAKE2b-256 |
fa5759223e7e1e050e9ffc06356c6d0b767504d498a016cee3ec73502f8d5845
|