mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-15 17:40:03 -05:00
* ➕ Add mkdocstrings and griffe-typingdoc to dependencies * 🔧 Add mkdocstrings configs to MkDocs * 📝 Add first WIP reference page * ⬆️ Upgrade typing-extensions to the minimum version including Doc() * 📝 Add docs to FastAPI parameters * 📝 Add docstrings for OpenAPI docs utils * 📝 Add docstrings for security utils * 📝 Add docstrings for UploadFile * 📝 Update docstrings in FastAPI class * 📝 Add docstrings for path operation methods * 📝 Add docstring for jsonable_encoder * 📝 Add docstrings for exceptions * 📝 Add docstsrings for parameter functions * 📝 Add docstrings for responses * 📝 Add docstrings for APIRouter * ♻️ Sub-class BackgroundTasks to document it with docstrings * 📝 Update usage of background tasks in dependencies * ✅ Update tests with new deprecation warnings * 📝 Add new reference docs * 🔧 Update MkDocs with new reference docs * ✅ Update pytest fixture, deprecation is raised only once * 🎨 Update format for types in exceptions.py * ♻️ Update annotations in BackgroundTask, `Annotated` can't take ParamSpec's P.args or P.kwargs * ✏️ Fix typos caught by @pawamoy * 🔧 Update and fix MkDocstrings configs from @pawamoy tips * 📝 Update reference docs * ✏️ Fix typos found by @pawamoy * ➕ Add HTTPX as a dependency for docs, for the TestClient * 🔧 Update MkDocs config, rename websockets reference * 🔇 Add type-ignores for Doc as the stubs haven't been released for mypy * 🔥 Remove duplicated deprecated notice * 🔇 Remove typing error for unreleased stub in openapi/docs.py * ✅ Add tests for UploadFile for coverage * ⬆️ Upgrade griffe-typingdoc==0.2.2 * 📝 Refactor docs structure * 🔨 Update README generation with new index frontmatter and style * 🔨 Update generation of languages, remove from top menu, keep in lang menu * 📝 Add OpenAPI Pydantic models * 🔨 Update docs script to not translate Reference and Release Notes * 🔧 Add reference for OpenAPI models * 🔧 Update MkDocs config for mkdocstrings insiders * 👷 Install mkdocstring insiders in CI for docs * 🐛 Fix MkDocstrings insiders install URL * ➕ Move dependencies shared by docs and tests to its own requirements file * 👷 Update cache keys for test and docs dependencies * 📝 Remove no longer needed __init__ placeholder docstrings * 📝 Move docstring for APIRouter to the class level (not __init__ level) * 🔥 Remove no longer needed dummy placeholder __init__ docstring
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from typing import Any, Callable
|
|
|
|
from starlette.background import BackgroundTasks as StarletteBackgroundTasks
|
|
from typing_extensions import Annotated, Doc, ParamSpec # type: ignore [attr-defined]
|
|
|
|
P = ParamSpec("P")
|
|
|
|
|
|
class BackgroundTasks(StarletteBackgroundTasks):
|
|
"""
|
|
A collection of background tasks that will be called after a response has been
|
|
sent to the client.
|
|
|
|
Read more about it in the
|
|
[FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
|
|
|
|
## Example
|
|
|
|
```python
|
|
from fastapi import BackgroundTasks, FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def write_notification(email: str, message=""):
|
|
with open("log.txt", mode="w") as email_file:
|
|
content = f"notification for {email}: {message}"
|
|
email_file.write(content)
|
|
|
|
|
|
@app.post("/send-notification/{email}")
|
|
async def send_notification(email: str, background_tasks: BackgroundTasks):
|
|
background_tasks.add_task(write_notification, email, message="some notification")
|
|
return {"message": "Notification sent in the background"}
|
|
```
|
|
"""
|
|
|
|
def add_task(
|
|
self,
|
|
func: Annotated[
|
|
Callable[P, Any],
|
|
Doc(
|
|
"""
|
|
The function to call after the response is sent.
|
|
|
|
It can be a regular `def` function or an `async def` function.
|
|
"""
|
|
),
|
|
],
|
|
*args: P.args,
|
|
**kwargs: P.kwargs,
|
|
) -> None:
|
|
"""
|
|
Add a function to be called in the background after the response is sent.
|
|
|
|
Read more about it in the
|
|
[FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
|
|
"""
|
|
return super().add_task(func, *args, **kwargs)
|