Skip to content

Commit

Permalink
Add support to style function definitions with newlines before functi…
Browse files Browse the repository at this point in the history
…on 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 <12621235+hauntsaninja@users.noreply.github.com>
  • Loading branch information
peterkra25 and hauntsaninja authored Apr 24, 2024
1 parent f4b644b commit 1354be2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

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

- Add support to style function definitions containing newlines before function stubs
(#4318)

### Performance

<!-- Changes that improve Black's performance. -->
Expand Down
7 changes: 5 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,11 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
yield from self.line(-1)

else:
if not node.parent or not is_stub_suite(node.parent):
yield from self.line()
if node.parent and is_stub_suite(node.parent):
node.prefix = ""
yield from self.visit_default(node)
return
yield from self.line()
yield from self.visit_default(node)

def visit_async_stmt(self, node: Node) -> Iterator[Line]:
Expand Down
105 changes: 105 additions & 0 deletions tests/data/cases/dummy_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,67 @@ async def async_function(self):
async def async_function(self):
...

class ClassA:
def f(self):

...


class ClassB:
def f(self):









...


class ClassC:
def f(self):

...
# Comment


class ClassD:
def f(self):# Comment 1

...# Comment 2
# Comment 3


class ClassE:
def f(self):

...
def f2(self):
print(10)


class ClassF:
def f(self):

...# Comment 2


class ClassG:
def f(self):#Comment 1

...# Comment 2


class ClassH:
def f(self):
#Comment

...


# output

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

@decorated
async def async_function(self): ...


class ClassA:
def f(self): ...


class ClassB:
def f(self): ...


class ClassC:
def f(self):

...
# Comment


class ClassD:
def f(self): # Comment 1

... # Comment 2
# Comment 3


class ClassE:
def f(self): ...
def f2(self):
print(10)


class ClassF:
def f(self): ... # Comment 2


class ClassG:
def f(self): # Comment 1
... # Comment 2


class ClassH:
def f(self):
# Comment

...

0 comments on commit 1354be2

Please sign in to comment.