fastapi
fastapi copied to clipboard
Raw docstring (leading `r`) defeats form feed `\f` truncation
Discussed in https://github.com/tiangolo/fastapi/discussions/10531
Originally posted by jamesbraza October 26, 2023
First Check
- [X] I added a very descriptive title here.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I searched the FastAPI documentation, with the integrated search.
- [X] I already searched in Google "How to X in FastAPI" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to FastAPI but to Pydantic.
- [X] I already checked if it is not related to FastAPI but to Swagger UI.
- [X] I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
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
Description
Running ruff==0.1.3
on this, D301
will autofix to make the docstring be a raw string (lead by r
).
@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 \f
truncation from working, per https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#advanced-description-from-docstring
I think form feed \f
truncation should still work even if the docstring is a raw string.
Operating System
macOS
Operating System Details
n/a
FastAPI Version
0.104.0
Pydantic Version
2.4.2
Python Version
3.11.5
Additional Context
No response
I little bit handled this by adding some code. I don't know what you actually want, but you want like this code situation?
Above code is with raw string that print to normal string. And also here is my code changed version.
Here is code about I write
Well, r"\f"
=="\\f"
!= "\f"
With change you propose to docstrings like This endpoint saves data to C:\files\data
will be truncated to This endpoint saves data to C:
opened the #11149 pr used codecs to convert rawstring so that it can be split on \f
self.description = codecs.decode(self.description, "unicode_escape")