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

Fix No blank line after if/for/while/def #1180

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions yapf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def main(argv):

source = [line.rstrip() for line in original_source]
source[0] = _removeBOM(source[0])
# filter all the tuples with empty space
source = list(filter(None, source))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this change, as it seems overly broad. For instance, if there are disabled sections, we don't want to remove the newlines from them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks! Yeah, makes sense to not format the disabled sections. I'll try to see if I can prevent the commented section from filtering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bwendling, I have added a util function that filters out the comment sections while removing the new lines. Kindly let me know for any feedback.


try:
reformatted_source, _ = yapf_api.FormatCode(
Expand Down
8 changes: 8 additions & 0 deletions yapftests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ def testHelp(self):
self.assertIn('indent_width=4', help_message)
self.assertIn('The number of spaces required before a trailing comment.',
help_message)
def testExtraBlankLine(self):
ashmichheda marked this conversation as resolved.
Show resolved Hide resolved
code = 'if True:\n\n\n\n\t print(2)'
yapf_code = 'if True:\n print(2)\n'
with patched_input(code):
with captured_output() as (out, _):
ret = yapf.main([])
self.assertEqual(ret, 0)
self.assertEqual(out.getvalue(), yapf_code)