Skip to content

Commit

Permalink
Fix #8: In multiline module docstrings, trailing quotes that are put …
Browse files Browse the repository at this point in the history
…on their own line are now kept on their own line.

Will publish a new release v23.3.1 after this is merged.

PiperOrigin-RevId: 521763971
  • Loading branch information
yilei committed Apr 4, 2023
1 parent 0c86c0f commit 6371280
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to Pyink are recorded here.

* Nothing notable unreleased.

## 23.3.1

* In multiline module docstrings, trailing quotes that are put on their own line
are now kept on their own line. (#8)

## 23.3.0

This release is based on Black v23.3.0.
Expand Down
14 changes: 13 additions & 1 deletion src/pyink/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
docstring_started_empty = not docstring
indent = " " * self.current_line.indentation_spaces()

original_has_trailing_newline = docstring.endswith("\n")
if is_multiline_string(leaf):
docstring = fix_docstring(docstring, indent)
else:
Expand Down Expand Up @@ -478,7 +479,9 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
last_line_length = (
# When docstring ends with '\n' the last line is empty,
# not the last item from splitlines().
len(lines[-1]) if docstring and not docstring.endswith("\n") else 0
len(lines[-1])
if docstring and not docstring.endswith("\n")
else 0
)

# If adding closing quotes would cause the last line to exceed
Expand All @@ -491,6 +494,15 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
and not has_trailing_backslash
):
leaf.value = prefix + quote + docstring + "\n" + indent + quote
elif (
not indent
and len(lines) > 1
and not docstring.endswith("\n")
and original_has_trailing_newline
):
# Special case for module docstrings that put trailing quotes on
# their own line.
leaf.value = prefix + quote + docstring + "\n" + indent + quote
else:
leaf.value = prefix + quote + docstring + quote
else:
Expand Down
5 changes: 5 additions & 0 deletions tests/data/pyink/module_docstring_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Regression test for https://github.com/google/pyink/issues/8.
"""Module docstrings with trailing quotes on their own line.
Just like other docstrings.
"""
4 changes: 4 additions & 0 deletions tests/data/pyink/module_docstring_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Regression test for https://github.com/google/pyink/issues/8.
"""
Module docstring on its own line.
"""

0 comments on commit 6371280

Please sign in to comment.