Raw docstring (leading r
) defeats form feed \f
truncation
#10531
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
app = FastAPI()
@app.post("/lof")
def foo(arg: int = 5) -> int:
"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg DescriptionRunning @app.post("/lof")
def foo(arg: int = 5) -> int:
r"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg However, this breaks the form feed I think form feed Operating SystemmacOS Operating System Detailsn/a FastAPI Version0.104.0 Pydantic Version2.4.2 Python Version3.11.5 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello @jamesbraza, Indeed, when using raw single quotes, FastAPI will not consider the character '\f,' resulting in the documentation not being truncated. According to the ruff documentation, the use of raw triple double quotes in Python docstrings is recommended in many cases, especially when you need to maintain characters that include backslashes literally. However, in the context of FastAPI, a solution can be found that allows the character '\f' to work as expected by using triple double quotes. This is clearly demonstrated in FastAPI's documentation link to FastAPI documentation. Here's an example of how to achieve this: from fastapi import FastAPI
app = FastAPI()
@app.post("/lof")
def foo(arg: int = 5) -> int:
"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg In this example, you'll notice that triple single quotes are used, and the '\f' character is correctly truncated in the documentation generated by FastAPI. To prevent ruff from interfering and marking the code as a syntax error, you can add the following configuration to your [tool.ruff]
select = ["D301"] FastAPI offers various ways to write documentation without being affected by this issue. One of them is using the openapi_extra parameter while keeping the docstring with raw triple double quotes: from fastapi import FastAPI
app = FastAPI()
@app.post("/lof", openapi_extra={
"tags": ["Tag for example routes"],
"summary": "Summary for the route",
"description": "Write the description of the route without characters that include backslashes, as they will not be considered when writing the OpenAPI documentation."
})
def foo(arg: int = 5) -> int:
r"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg Another option is to write the documentation in another file (as a constant) and add it to the route decorator in this way: # In another file named docs.py
MY_DOC = {
"tags": ["Tag for example routes"],
"summary": "Summary for the route",
"description": "For this example, you can use characters that include backslashes, as when using this method, they will be taken into account when writing the OpenAPI documentation\f.This text will not be considered."
}
# In your main file
from fastapi import FastAPI
from my_constants import MY_DOC
app = FastAPI()
@app.post("/lof", **MY_DOC)
def foo(arg: int = 5) -> int:
r"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg You can also add parameters directly to the route decorator, such as from fastapi import FastAPI
app = FastAPI()
@app.post(
"/",
tags=["Tag for example routes"],
summary="Summary for the route",
description="For this example, you can use characters that include backslashes, as when using this method, they will be taken into account when writing the OpenAPI documentation\f.This text will not be considered."
)
def foo(arg: int = 5) -> int:
"""
Some function.
\f
Args:
arg: Some argument
Returns:
Some integer.
"""
return arg These are the solutions I've found to address this issue. Another option would be to ask @tiangolo to allow us to create an issue to request the consideration of supporting raw triple double quotes in the documentation. |
Beta Was this translation helpful? Give feedback.
-
I've created this issue: #10998 |
Beta Was this translation helpful? Give feedback.
I've created this issue: #10998