Skip to content

Commit 7f350b0

Browse files
authored
Don't add newlines to the start of docstrings (#2)
1 parent c621510 commit 7f350b0

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

add_docstrings.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DocumentableObject(NamedTuple):
4646

4747

4848
def clean_docstring(docstring: str) -> str:
49-
return docstring.strip().replace("\\", "\\\\")
49+
return docstring.rstrip().replace("\\", "\\\\")
5050

5151

5252
def add_docstring_to_libcst_node(
@@ -77,7 +77,23 @@ def add_docstring_to_libcst_node(
7777
return updated_node
7878

7979
indentation = " " * 4 * level
80-
indented_docstring = f'"""\n{textwrap.indent(clean_docstring(docstring), indentation)}\n{indentation}"""'
80+
81+
# For a single-line docstring, this can result in funny things like:
82+
#
83+
# ```py
84+
# def foo():
85+
# """A docstring.
86+
# """
87+
# ```
88+
#
89+
# But we don't need to worry about that here: Black sorts that out for us and turns it into:
90+
#
91+
# ```py
92+
# def foo():
93+
# """A docstring."""
94+
# ```
95+
indented_docstring = f'"""{textwrap.indent(clean_docstring(docstring), indentation).lstrip(" ")}\n{indentation}"""'
96+
8197
docstring_node = libcst.Expr(libcst.SimpleString(indented_docstring))
8298

8399
# If the body is just a `...`, replace it with just the docstring.

0 commit comments

Comments
 (0)