Skip to content

Commit

Permalink
fix crashes on docstring whitespace changes (#1417)
Browse files Browse the repository at this point in the history
Fixes #1415
  • Loading branch information
JelleZijlstra authored May 16, 2020
1 parent 45c98cf commit c7da348
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6006,13 +6006,14 @@ def _stringify_ast(

else:
# Constant strings may be indented across newlines, if they are
# docstrings; fold spaces after newlines when comparing
# docstrings; fold spaces after newlines when comparing. Similarly,
# trailing and leading space may be removed.
if (
isinstance(node, ast.Constant)
and field == "value"
and isinstance(value, str)
):
normalized = re.sub(r"\n[ \t]+", "\n ", value)
normalized = re.sub(r" *\n[ \t]+", "\n ", value).strip()
else:
normalized = value
yield f"{' ' * (depth+2)}{normalized!r}, # {value.__class__.__name__}"
Expand Down
16 changes: 14 additions & 2 deletions tests/data/docstring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MyClass:
"""Multiline
""" Multiline
class docstring
"""

Expand All @@ -11,7 +11,7 @@ def method(self):


def foo():
"""This is a docstring with
"""This is a docstring with
some lines of text here
"""
return
Expand Down Expand Up @@ -66,6 +66,13 @@ def over_indent():
"""
pass


def single_line():
"""But with a newline after it!
"""
pass

# output

class MyClass:
Expand Down Expand Up @@ -136,3 +143,8 @@ def over_indent():
- And the closing quote is too deep
"""
pass


def single_line():
"""But with a newline after it!"""
pass

0 comments on commit c7da348

Please sign in to comment.