Skip to content

Commit

Permalink
Fix empty line handling when formatting typing stubs
Browse files Browse the repository at this point in the history
Black used to erroneously remove all empty lines between functions
preceded by decorators and non-function code in pyi mode. Now a single
empty line is enforced.
  • Loading branch information
ichard26 committed Aug 28, 2020
1 parent 2b75f88 commit dc39a82
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,9 @@
- `Black` now respects `--skip-string-normalization` when normalizing multiline
docstring quotes (#1637)

- `Black` no longer removes all empty lines between decorators and non-function code
when formatting typing stubs. Now `Black` enforces a single empty line. (#1646)

- fixed a crash when PWD=/ on POSIX (#1631)

### 20.8b1
Expand Down
5 changes: 5 additions & 0 deletions docs/change_log.md
Expand Up @@ -9,6 +9,11 @@
- `Black` now respects `--skip-string-normalization` when normalizing multiline
docstring quotes (#1637)

- `Black` no longer removes all empty lines between decorators and non-function code
when formatting typing stubs. Now `Black` enforces a single empty line. (#1646)

- fixed a crash when PWD=/ on POSIX (#1631)

### 20.8b1

#### _Packaging_
Expand Down
7 changes: 5 additions & 2 deletions src/black/__init__.py
Expand Up @@ -1847,8 +1847,11 @@ def _maybe_empty_lines_for_class_or_def(
newlines = 0
else:
newlines = 1
elif current_line.is_def and not self.previous_line.is_def:
# Blank line between a block of functions and a block of non-functions
elif (
current_line.is_def or current_line.is_decorator
) and not self.previous_line.is_def:
# Blank line between a block of functions (maybe with preceding
# decorators) and a block of non-functions
newlines = 1
else:
newlines = 0
Expand Down
6 changes: 6 additions & 0 deletions tests/data/force_pyi.py
@@ -1,6 +1,12 @@
import a

@b
def f(): ...

def g(): ...
# output
import a

@b
def f(): ...
def g(): ...
7 changes: 4 additions & 3 deletions tests/test_black.py
Expand Up @@ -1527,7 +1527,6 @@ def test_tricky_unicode_symbols(self) -> None:
black.assert_stable(source, actual, DEFAULT_MODE)

def test_single_file_force_pyi(self) -> None:
reg_mode = DEFAULT_MODE
pyi_mode = replace(DEFAULT_MODE, is_pyi=True)
contents, expected = read_data("force_pyi")
with cache_dir() as workspace:
Expand All @@ -1540,9 +1539,11 @@ def test_single_file_force_pyi(self) -> None:
# verify cache with --pyi is separate
pyi_cache = black.read_cache(pyi_mode)
self.assertIn(path, pyi_cache)
normal_cache = black.read_cache(reg_mode)
normal_cache = black.read_cache(DEFAULT_MODE)
self.assertNotIn(path, normal_cache)
self.assertEqual(actual, expected)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(contents, actual)
black.assert_stable(contents, actual, pyi_mode)

@event_loop()
def test_multi_file_force_pyi(self) -> None:
Expand Down

0 comments on commit dc39a82

Please sign in to comment.