From dc39a82105e0e7119613a1f918762a8decc74353 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Thu, 27 Aug 2020 21:54:19 -0400 Subject: [PATCH] Fix empty line handling when formatting typing stubs 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. --- CHANGES.md | 3 +++ docs/change_log.md | 5 +++++ src/black/__init__.py | 7 +++++-- tests/data/force_pyi.py | 6 ++++++ tests/test_black.py | 7 ++++--- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1c53604d4d5..4af9ac46006 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/docs/change_log.md b/docs/change_log.md index b7337166659..9114f13c398 100644 --- a/docs/change_log.md +++ b/docs/change_log.md @@ -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_ diff --git a/src/black/__init__.py b/src/black/__init__.py index 048e771ce96..8bd376c5ec9 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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 diff --git a/tests/data/force_pyi.py b/tests/data/force_pyi.py index 25246c22ca7..1440d2f7334 100644 --- a/tests/data/force_pyi.py +++ b/tests/data/force_pyi.py @@ -1,6 +1,12 @@ +import a + +@b def f(): ... def g(): ... # output +import a + +@b def f(): ... def g(): ... diff --git a/tests/test_black.py b/tests/test_black.py index bc80c8fca8b..4d054584780 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -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: @@ -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: