Skip to content

Commit 1354be2

Browse files
Add support to style function definitions with newlines before function stubs (#4318)
* Add support to style function definitions containing newlines before function stubs * Relocated implementation for removal of newlines before function stubs with added tests for comments --------- Co-authored-by: Shantanu <[email protected]>
1 parent f4b644b commit 1354be2

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
<!-- Changes to the parser or to version autodetection -->
3030

31+
- Add support to style function definitions containing newlines before function stubs
32+
(#4318)
33+
3134
### Performance
3235

3336
<!-- Changes that improve Black's performance. -->

src/black/linegen.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
309309
yield from self.line(-1)
310310

311311
else:
312-
if not node.parent or not is_stub_suite(node.parent):
313-
yield from self.line()
312+
if node.parent and is_stub_suite(node.parent):
313+
node.prefix = ""
314+
yield from self.visit_default(node)
315+
return
316+
yield from self.line()
314317
yield from self.visit_default(node)
315318

316319
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+
...

0 commit comments

Comments
 (0)