How to fix SyntaxWarning: invalid escape sequence in Python? For convenience, you can use the following method to automatically add r to docstrings: write a script using e g libcst to parse the source code, modify it to add r at appropriate places, then write it back run the script on your code The advantage of using CST over AST is for example number of parentheses or white spaces are preserved