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

Make black remove leading and trailing spaces from one-line docstrings: fixes #1738 #1812 #1740

Merged
merged 17 commits into from Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@

#### _Black_

- `Black` now requires one-line docstrings to be stripped (PR #1740)
MarkCBell marked this conversation as resolved.
Show resolved Hide resolved

- `Black` now respects `--skip-string-normalization` when normalizing multiline
docstring quotes (#1637)

Expand Down
15 changes: 7 additions & 8 deletions src/black/__init__.py
Expand Up @@ -2138,8 +2138,12 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
prefix = get_string_prefix(leaf.value)
lead_len = len(prefix) + 3
tail_len = -3
indent = " " * 4 * self.current_line.depth
docstring = fix_docstring(leaf.value[lead_len:tail_len], indent)
docstring = leaf.value[lead_len:tail_len]
if is_multiline_string(leaf):
indent = " " * 4 * self.current_line.depth
docstring = fix_docstring(docstring, indent)
else:
docstring = docstring.strip()
if docstring:
if leaf.value[lead_len - 1] == docstring[0]:
docstring = " " + docstring
Expand Down Expand Up @@ -6089,7 +6093,7 @@ def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:

@lru_cache()
def get_gitignore(root: Path) -> PathSpec:
""" Return a PathSpec matching gitignore content if present."""
"""Return a PathSpec matching gitignore content if present."""
gitignore = root / ".gitignore"
lines: List[str] = []
if gitignore.is_file():
Expand Down Expand Up @@ -6908,11 +6912,6 @@ def patched_main() -> None:


def is_docstring(leaf: Leaf) -> bool:
if not is_multiline_string(leaf):
# For the purposes of docstring re-indentation, we don't need to do anything
# with single-line docstrings.
return False

if prev_siblings_are(
leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
):
Expand Down
9 changes: 7 additions & 2 deletions tests/data/docstring.py
Expand Up @@ -110,6 +110,8 @@ def ignored_docstring():
"""a => \
b"""

def single_line_docstring_with_whitespace():
""" This should be stripped """

def docstring_with_inline_tabs_and_space_indentation():
"""hey
Expand All @@ -134,7 +136,6 @@ def docstring_with_inline_tabs_and_tab_indentation():
line ends with some tabs
"""
pass


# output

Expand Down Expand Up @@ -251,6 +252,10 @@ def ignored_docstring():
b"""


def single_line_docstring_with_whitespace():
"""This should be stripped"""


def docstring_with_inline_tabs_and_space_indentation():
"""hey

Expand All @@ -273,4 +278,4 @@ def docstring_with_inline_tabs_and_tab_indentation():

line ends with some tabs
"""
pass
pass