Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to style function definitions with newlines before function stubs #4318

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -26,6 +26,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
Expand Up @@ -313,8 +313,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
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

...