From 38648b7d4bdf1005417aed10d5846edbd29797eb Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Fri, 28 Jan 2022 13:26:58 -0800 Subject: [PATCH 1/4] more tests for stubs --- tests/data/stub.pyi | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/data/stub.pyi b/tests/data/stub.pyi index 9a246211284..b2a93ddc75f 100644 --- a/tests/data/stub.pyi +++ b/tests/data/stub.pyi @@ -32,6 +32,42 @@ def g(): def h(): ... +if sys.version_info >= (3, 8): + class E: + def f(self): ... + class F: + + def f(self): ... + class G: ... + class H: ... +else: + class I: ... + class J: ... + def f(): ... + + class K: + def f(self): ... + def f(): ... + +class Nested: + class dirty: ... + class little: ... + class secret: + def who_has_to_know(self): ... + def verse(self): ... + +class CondMethod: + def f(self): ... + if sys.version_info >= (3, 8): + def g(self): ... + else: + def g(self): ... + def h(self): ... + def i(self): ... + if sys.version_info >= (3, 8): + def j(self): ... + def k(self): ... + # output X: int @@ -56,3 +92,38 @@ class A: def g(): ... def h(): ... + +if sys.version_info >= (3, 8): + class E: + def f(self): ... + class F: + def f(self): ... + class G: ... + class H: ... + +else: + class I: ... + class J: ... + def f(): ... + class K: + def f(self): ... + def f(): ... + +class Nested: + class dirty: ... + class little: ... + class secret: + def who_has_to_know(self): ... + def verse(self): ... + +class CondMethod: + def f(self): ... + if sys.version_info >= (3, 8): + def g(self): ... + else: + def g(self): ... + def h(self): ... + def i(self): ... + if sys.version_info >= (3, 8): + def j(self): ... + def k(self): ... From ea42d01092fe535c55bbb8cf84edb0419601f03d Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Fri, 28 Jan 2022 13:28:33 -0800 Subject: [PATCH 2/4] change how blank lines work --- src/black/lines.py | 10 ++++++---- tests/data/stub.pyi | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 7d50f02aebc..1c4e38a96c1 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -530,11 +530,11 @@ def _maybe_empty_lines_for_class_or_def( return 0, 0 if self.is_pyi: - if self.previous_line.depth > current_line.depth: - newlines = 0 if current_line.depth else 1 - elif current_line.is_class or self.previous_line.is_class: - if current_line.depth: + if current_line.is_class or self.previous_line.is_class: + if self.previous_line.depth < current_line.depth: newlines = 0 + elif self.previous_line.depth > current_line.depth: + newlines = 1 elif current_line.is_stub_class and self.previous_line.is_stub_class: # No blank line between classes with an empty body newlines = 0 @@ -551,6 +551,8 @@ def _maybe_empty_lines_for_class_or_def( # Blank line between a block of functions (maybe with preceding # decorators) and a block of non-functions newlines = 1 + elif self.previous_line.depth > current_line.depth: + newlines = 1 else: newlines = 0 else: diff --git a/tests/data/stub.pyi b/tests/data/stub.pyi index b2a93ddc75f..9342d6a71eb 100644 --- a/tests/data/stub.pyi +++ b/tests/data/stub.pyi @@ -96,24 +96,31 @@ def h(): ... if sys.version_info >= (3, 8): class E: def f(self): ... + class F: def f(self): ... + class G: ... class H: ... else: class I: ... class J: ... + def f(): ... + class K: def f(self): ... + def f(): ... class Nested: class dirty: ... class little: ... + class secret: def who_has_to_know(self): ... + def verse(self): ... class CondMethod: @@ -122,8 +129,10 @@ class CondMethod: def g(self): ... else: def g(self): ... + def h(self): ... def i(self): ... if sys.version_info >= (3, 8): def j(self): ... + def k(self): ... From 68193b9ebd8fefb64b413f3c07c8d036ae07720a Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Fri, 28 Jan 2022 14:07:26 -0800 Subject: [PATCH 3/4] changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 440aaeaa35a..274c5640ec0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,7 @@ and the first release covered by our new stability policy. - Use parentheses for attribute access on decimal float and int literals (#2799) - Don't add whitespace for attribute access on hexadecimal, binary, octal, and complex literals (#2799) +- Treat blank lines in stubs the same inside top-level `if` statements (#2820) ### Parser From 01f4b2286e25820a61188185e1f1427417eebfa0 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Fri, 28 Jan 2022 15:58:59 -0800 Subject: [PATCH 4/4] add conditional nested class --- tests/data/stub.pyi | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/data/stub.pyi b/tests/data/stub.pyi index 9342d6a71eb..af2cd2c2c02 100644 --- a/tests/data/stub.pyi +++ b/tests/data/stub.pyi @@ -56,7 +56,7 @@ class Nested: def who_has_to_know(self): ... def verse(self): ... -class CondMethod: +class Conditional: def f(self): ... if sys.version_info >= (3, 8): def g(self): ... @@ -67,6 +67,12 @@ class CondMethod: if sys.version_info >= (3, 8): def j(self): ... def k(self): ... + if sys.version_info >= (3, 8): + class A: ... + class B: ... + class C: + def l(self): ... + def m(self): ... # output @@ -123,7 +129,7 @@ class Nested: def verse(self): ... -class CondMethod: +class Conditional: def f(self): ... if sys.version_info >= (3, 8): def g(self): ... @@ -136,3 +142,10 @@ class CondMethod: def j(self): ... def k(self): ... + if sys.version_info >= (3, 8): + class A: ... + class B: ... + + class C: + def l(self): ... + def m(self): ...