From c7da3482c7badf5c6d8c0c9a37495ab4529eaaad Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 15 May 2020 20:47:21 -0700 Subject: [PATCH] fix crashes on docstring whitespace changes (#1417) Fixes #1415 --- src/black/__init__.py | 5 +++-- tests/data/docstring.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 41c688b..77dda2b 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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__}" diff --git a/tests/data/docstring.py b/tests/data/docstring.py index dd5bab7..f5adeb7 100644 --- a/tests/data/docstring.py +++ b/tests/data/docstring.py @@ -1,5 +1,5 @@ class MyClass: - """Multiline + """ Multiline class docstring """ @@ -11,7 +11,7 @@ class MyClass: def foo(): - """This is a docstring with + """This is a docstring with some lines of text here """ return @@ -66,6 +66,13 @@ def over_indent(): """ pass + +def single_line(): + """But with a newline after it! + + """ + pass + # output class MyClass: @@ -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 -- 2.39.5