Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crashes on docstring whitespace changes #1417

Merged
merged 1 commit into from
May 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
@@ -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__}"
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
"""

@@ -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
@@ -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
Comment on lines +146 to +150

This comment was marked as resolved.

Copy link
Collaborator

@ichard26 ichard26 May 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, didn't see the output comment. Sorry about the unnecessary notifications.