Skip to content

Commit

Permalink
Merge 466767d into fb8dfde
Browse files Browse the repository at this point in the history
  • Loading branch information
saroad2 committed Apr 29, 2022
2 parents fb8dfde + 466767d commit d83fad7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Multiple contributions by:
- [Rishikesh Jha](mailto:rishijha424@gmail.com)
- [Rupert Bedford](mailto:rupert@rupertb.com)
- Russell Davis
- [Sagi Shadur](mailto:saroad2@gmail.com)
- [Rémi Verschelde](mailto:rverschelde@gmail.com)
- [Sami Salonen](mailto:sakki@iki.fi)
- [Samuel Cormier-Iijima](mailto:samuel@cormier-iijima.com)
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Remove redundant parentheses around awaited objects (#2991)
- Parentheses around return annotations are now managed (#2990)
- Remove unnecessary parentheses from `with` statements (#2926)
- Remove trailing newlines after `def` lines (#3035)

### _Blackd_

Expand Down
7 changes: 7 additions & 0 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
):
return before, 1

if (
Preview.remove_def_trailing_newline in current_line.mode
and self.previous_line
and self.previous_line.is_def
and depth == self.previous_line.depth + 1
):
return 0, 0
return before, 0

def _maybe_empty_lines_for_class_or_def(
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Preview(Enum):
remove_redundant_parens = auto()
one_element_subscript = auto()
annotation_parens = auto()
remove_def_trailing_newline = auto()


class Deprecated(UserWarning):
Expand Down
8 changes: 8 additions & 0 deletions tests/data/newline_after_def1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def foo():

print("The newline above me should be deleted!")

# output

def foo():
print("The newline above me should be deleted!")
10 changes: 10 additions & 0 deletions tests/data/newline_after_def2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def foo():



print("All the newlines above me should be deleted!")

# output

def foo():
print("All the newlines above me should be deleted!")
12 changes: 12 additions & 0 deletions tests/data/newline_after_def3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def f():

print("No newline above me!")

print("There is a newline above me, and that's OK!")

# output

def f():
print("No newline above me!")

print("There is a newline above me, and that's OK!")
12 changes: 12 additions & 0 deletions tests/data/newline_after_def4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def foo():

# There is a comment here

print("The newline above me should not be deleted!")

# output

def foo():
# There is a comment here

print("The newline above me should not be deleted!")
10 changes: 10 additions & 0 deletions tests/data/newline_after_def5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo:
def bar(self):

print("The newline above me should be deleted!")

# output

class Foo:
def bar(self):
print("The newline above me should be deleted!")
1 change: 0 additions & 1 deletion tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,6 @@ def test_newline_comment_interaction(self) -> None:
black.assert_stable(source, output, mode=DEFAULT_MODE)

def test_bpo_2142_workaround(self) -> None:

# https://bugs.python.org/issue2142

source, _ = read_data("missing_final_newline.py")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"cantfit",
"comments7",
"comments8",
"newline_after_def1",
"newline_after_def2",
"newline_after_def3",
"newline_after_def4",
"newline_after_def5",
"long_strings",
"long_strings__edge_case",
"long_strings__regression",
Expand Down

0 comments on commit d83fad7

Please sign in to comment.