Describe the bug
When generating (class) docstrings, special characters such as backslash are not properly escaped.
This causes a warning in type checkers such as pyright/Pylance, and may cause a syntax error in future Python versions.
To Reproduce
Example schema:
{
"openapi": "3.0.0",
"components": {
"schemas": {
"Example": {
"description": "Observe the backslash \\ in this line",
"type": "object"
}
}
}
}
Used commandline:
$ datamodel-codegen --input example.json --use-schema-description
Actual behavior
The backslash in the produced docstring is not escaped:
class Example(BaseModel):
"""
Observe the backslash \ in this line
"""
For example, in Pylance, this will generate the warning reportInvalidStringEscapeSequence:
Unsupported escape sequence in string literal
Expected behavior
The backslash should be escaped, like this:
class Example(BaseModel):
"""
Observe the backslash \\ in this line
"""
Version:
Additional context
I only checked class docstrings, but it could affect field docstrings as well.
This is not limited to backslashes. An unescaped triple quote even causes a black parsing error.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too