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

Function docstring newlines #1

Open
wants to merge 8 commits into
base: module_docstring_newlines
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- Remove unnecessary parentheses from `with` statements (#2926)
- Standardise newlines after module-level docstrings (#2996)
- Remove newlines after function docstrings (#1)

### _Blackd_

Expand Down
2 changes: 0 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,6 @@ def maybe_make_parens_invisible_in_atom(

def should_split_line(line: Line, opening_bracket: Leaf) -> bool:
"""Should `line` be immediately split with `delimiter_split()` after RHS?"""

if not (opening_bracket.parent and opening_bracket.value in "[{("):
return False

Expand Down Expand Up @@ -1034,7 +1033,6 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
set is empty, unless the line should explode, in which case bracket pairs until
the one that needs to explode are omitted.
"""

omit: Set[LeafID] = set()
if not line.magic_trailing_comma:
yield omit
Expand Down
11 changes: 11 additions & 0 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,19 @@ def _maybe_empty_lines(self, current_line: Line) -> int:
and self.previous_lines[-1].is_triple_quoted_string
and current_line.depth == self.previous_lines[-1].depth
):
# Single newline after class docstring.
return 1

if (
self.preview
and len(self.previous_lines) > 1
and self.previous_lines[-2].is_def
and self.previous_lines[-1].is_triple_quoted_string
and current_line.depth == self.previous_lines[-1].depth
):
# No newline after function docstring.
return 0

return before

def _maybe_empty_lines_for_class_or_def(
Expand Down
1 change: 0 additions & 1 deletion src/black/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ def _normalize(lineend: str, value: str) -> str:

def stringify_ast(node: Union[ast.AST, ast3.AST], depth: int = 0) -> Iterator[str]:
"""Simple visitor generating strings to compare ASTs by content."""

node = fixup_ast_constants(node)

yield f"{' ' * depth}{node.__class__.__name__}("
Expand Down
1 change: 0 additions & 1 deletion src/black/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def TErr(err_msg: str) -> Err[CannotTransform]:

def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]:
"""A transformer which normalizes spacing around power operators."""

# Performance optimization to avoid unnecessary Leaf clones and other ops.
for leaf in line.leaves:
if leaf.type == token.DOUBLESTAR:
Expand Down
30 changes: 30 additions & 0 deletions tests/data/function_docstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def foo():
"""hello world."""



print("hello world!")


def bar(x: int, y: int) -> int:
"""hello world.

blah blah blah...
"""



return x + y

# output
def foo():
"""hello world."""
print("hello world!")


def bar(x: int, y: int) -> int:
"""hello world.

blah blah blah...
"""
return x + y
1 change: 1 addition & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"one_element_subscript",
"module_docstring_1",
"module_docstring_2",
"function_docstring",
]

SOURCES: List[str] = [
Expand Down