Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3437950

Browse files
authored
Treat blank lines in stubs the same inside top-level if statements (psf#2820)
1 parent e150676 commit 3437950

3 files changed

Lines changed: 100 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and the first release covered by our new stability policy.
2323
- Use parentheses for attribute access on decimal float and int literals (#2799)
2424
- Don't add whitespace for attribute access on hexadecimal, binary, octal, and complex
2525
literals (#2799)
26+
- Treat blank lines in stubs the same inside top-level `if` statements (#2820)
2627

2728
### Parser
2829

src/black/lines.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,11 @@ def _maybe_empty_lines_for_class_or_def(
530530
return 0, 0
531531

532532
if self.is_pyi:
533-
if self.previous_line.depth > current_line.depth:
534-
newlines = 0 if current_line.depth else 1
535-
elif current_line.is_class or self.previous_line.is_class:
536-
if current_line.depth:
533+
if current_line.is_class or self.previous_line.is_class:
534+
if self.previous_line.depth < current_line.depth:
537535
newlines = 0
536+
elif self.previous_line.depth > current_line.depth:
537+
newlines = 1
538538
elif current_line.is_stub_class and self.previous_line.is_stub_class:
539539
# No blank line between classes with an empty body
540540
newlines = 0
@@ -551,6 +551,8 @@ def _maybe_empty_lines_for_class_or_def(
551551
# Blank line between a block of functions (maybe with preceding
552552
# decorators) and a block of non-functions
553553
newlines = 1
554+
elif self.previous_line.depth > current_line.depth:
555+
newlines = 1
554556
else:
555557
newlines = 0
556558
else:

tests/data/stub.pyi

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,48 @@ def g():
3232

3333
def h(): ...
3434

35+
if sys.version_info >= (3, 8):
36+
class E:
37+
def f(self): ...
38+
class F:
39+
40+
def f(self): ...
41+
class G: ...
42+
class H: ...
43+
else:
44+
class I: ...
45+
class J: ...
46+
def f(): ...
47+
48+
class K:
49+
def f(self): ...
50+
def f(): ...
51+
52+
class Nested:
53+
class dirty: ...
54+
class little: ...
55+
class secret:
56+
def who_has_to_know(self): ...
57+
def verse(self): ...
58+
59+
class Conditional:
60+
def f(self): ...
61+
if sys.version_info >= (3, 8):
62+
def g(self): ...
63+
else:
64+
def g(self): ...
65+
def h(self): ...
66+
def i(self): ...
67+
if sys.version_info >= (3, 8):
68+
def j(self): ...
69+
def k(self): ...
70+
if sys.version_info >= (3, 8):
71+
class A: ...
72+
class B: ...
73+
class C:
74+
def l(self): ...
75+
def m(self): ...
76+
3577

3678
# output
3779
X: int
@@ -56,3 +98,54 @@ class A:
5698

5799
def g(): ...
58100
def h(): ...
101+
102+
if sys.version_info >= (3, 8):
103+
class E:
104+
def f(self): ...
105+
106+
class F:
107+
def f(self): ...
108+
109+
class G: ...
110+
class H: ...
111+
112+
else:
113+
class I: ...
114+
class J: ...
115+
116+
def f(): ...
117+
118+
class K:
119+
def f(self): ...
120+
121+
def f(): ...
122+
123+
class Nested:
124+
class dirty: ...
125+
class little: ...
126+
127+
class secret:
128+
def who_has_to_know(self): ...
129+
130+
def verse(self): ...
131+
132+
class Conditional:
133+
def f(self): ...
134+
if sys.version_info >= (3, 8):
135+
def g(self): ...
136+
else:
137+
def g(self): ...
138+
139+
def h(self): ...
140+
def i(self): ...
141+
if sys.version_info >= (3, 8):
142+
def j(self): ...
143+
144+
def k(self): ...
145+
if sys.version_info >= (3, 8):
146+
class A: ...
147+
class B: ...
148+
149+
class C:
150+
def l(self): ...
151+
def m(self): ...

0 commit comments

Comments
 (0)