Skip to content

Commit 8dc6217

Browse files
committed
Relocated implementation for removal of newlines before function stubs with added tests for comments
1 parent 40458d7 commit 8dc6217

4 files changed

Lines changed: 110 additions & 26 deletions

File tree

src/black/linegen.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ def visit_default(self, node: LN) -> Iterator[Line]:
159159
normalize_numeric_literal(node)
160160
if node.type not in WHITESPACE:
161161
self.current_line.append(node)
162-
if node.type == token.DOT:
163-
node.prefix = node.prefix.lstrip("\n")
164162
yield from super().visit_default(node)
165163

166164
def visit_test(self, node: Node) -> Iterator[Line]:
@@ -315,8 +313,11 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
315313
yield from self.line(-1)
316314

317315
else:
318-
if not node.parent or not is_stub_suite(node.parent):
319-
yield from self.line()
316+
if node.parent and is_stub_suite(node.parent):
317+
node.prefix = ""
318+
yield from self.visit_default(node)
319+
return
320+
yield from self.line()
320321
yield from self.visit_default(node)
321322

322323
def visit_async_stmt(self, node: Node) -> Iterator[Line]:

tests/data/cases/dummy_implementations.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,67 @@ async def async_function(self):
6868
async def async_function(self):
6969
...
7070

71+
class ClassA:
72+
def f(self):
73+
74+
...
75+
76+
77+
class ClassB:
78+
def f(self):
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
...
89+
90+
91+
class ClassC:
92+
def f(self):
93+
94+
...
95+
# Comment
96+
97+
98+
class ClassD:
99+
def f(self):# Comment 1
100+
101+
...# Comment 2
102+
# Comment 3
103+
104+
105+
class ClassE:
106+
def f(self):
107+
108+
...
109+
def f2(self):
110+
print(10)
111+
112+
113+
class ClassF:
114+
def f(self):
115+
116+
...# Comment 2
117+
118+
119+
class ClassG:
120+
def f(self):#Comment 1
121+
122+
...# Comment 2
123+
124+
125+
class ClassH:
126+
def f(self):
127+
#Comment
128+
129+
...
130+
131+
71132
# output
72133

73134
from typing import NoReturn, Protocol, Union, overload
@@ -142,3 +203,47 @@ async def async_function(self): ...
142203

143204
@decorated
144205
async def async_function(self): ...
206+
207+
208+
class ClassA:
209+
def f(self): ...
210+
211+
212+
class ClassB:
213+
def f(self): ...
214+
215+
216+
class ClassC:
217+
def f(self):
218+
219+
...
220+
# Comment
221+
222+
223+
class ClassD:
224+
def f(self): # Comment 1
225+
226+
... # Comment 2
227+
# Comment 3
228+
229+
230+
class ClassE:
231+
def f(self): ...
232+
def f2(self):
233+
print(10)
234+
235+
236+
class ClassF:
237+
def f(self): ... # Comment 2
238+
239+
240+
class ClassG:
241+
def f(self): # Comment 1
242+
... # Comment 2
243+
244+
245+
class ClassH:
246+
def f(self):
247+
# Comment
248+
249+
...

tests/data/cases/stub_empty_line.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/data/cases/stub_many_empty_lines.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)