Skip to content

Commit

Permalink
Follow PEP8: Method definitions inside a class are surrounded by a si…
Browse files Browse the repository at this point in the history
…ngle blank line (#954)

* PEP8: Method definitions inside a class are surrounded by a single blank line.

* Add changelog entry for "pep8" style update
  • Loading branch information
bouweandela committed Aug 23, 2021
1 parent da0dbb3 commit 5fda04e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Expand Up @@ -6,6 +6,10 @@
### Added
- Look at the 'pyproject.toml' file to see if it contains ignore file information
for YAPF.
### Fixed
- Enable `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF` knob for "pep8" style, so
method definitions inside a class are surrounded by a single blank line as
prescribed by PEP8.

## [0.31.0] 2021-03-14
### Added
Expand Down
4 changes: 2 additions & 2 deletions yapf/yapflib/style.py
Expand Up @@ -418,7 +418,7 @@ def CreatePEP8Style():
ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=True,
ALLOW_SPLIT_BEFORE_DICT_VALUE=True,
ARITHMETIC_PRECEDENCE_INDICATION=False,
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False,
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=True,
BLANK_LINE_BEFORE_CLASS_DOCSTRING=False,
BLANK_LINE_BEFORE_MODULE_DOCSTRING=False,
BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=2,
Expand Down Expand Up @@ -479,7 +479,6 @@ def CreateGoogleStyle():
"""Create the Google formatting style."""
style = CreatePEP8Style()
style['ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT'] = False
style['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'] = True
style['COLUMN_LIMIT'] = 80
style['INDENT_DICTIONARY_VALUE'] = True
style['INDENT_WIDTH'] = 4
Expand Down Expand Up @@ -511,6 +510,7 @@ def CreateFacebookStyle():
"""Create the Facebook formatting style."""
style = CreatePEP8Style()
style['ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT'] = False
style['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'] = False
style['COLUMN_LIMIT'] = 80
style['DEDENT_CLOSING_BRACKETS'] = True
style['INDENT_CLOSING_BRACKETS'] = False
Expand Down
1 change: 1 addition & 0 deletions yapftests/reformatter_basic_test.py
Expand Up @@ -1982,6 +1982,7 @@ def testMultilineDictionaryKeys(self):
def testStableDictionaryFormatting(self):
code = textwrap.dedent("""\
class A(object):
def method(self):
filters = {
'expressions': [{
Expand Down
12 changes: 9 additions & 3 deletions yapftests/reformatter_pep8_test.py
Expand Up @@ -50,22 +50,22 @@ def testSingleLineIfStatements(self):
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))

def testNoBlankBetweenClassAndDef(self):
def testBlankBetweenClassAndDef(self):
unformatted_code = textwrap.dedent("""\
class Foo:
def joe():
pass
""")
expected_formatted_code = textwrap.dedent("""\
class Foo:
def joe():
pass
""")
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testNoBlankBetweenDefsInClass(self):
def testBlankBetweenDefsInClass(self):
unformatted_code = textwrap.dedent('''\
class TestClass:
def __init__(self):
Expand All @@ -77,6 +77,7 @@ def is_running(self):
''')
expected_formatted_code = textwrap.dedent('''\
class TestClass:
def __init__(self):
self.running = False
Expand Down Expand Up @@ -174,6 +175,7 @@ def g():
""")
expected_formatted_code = textwrap.dedent("""\
def f():
def g():
while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa'
and xxxxxxxxxxxxxxxxxxxx(
Expand Down Expand Up @@ -341,11 +343,13 @@ def testSplitListsAndDictSetMakersIfCommaTerminated(self):
def testSplitAroundNamedAssigns(self):
unformatted_code = textwrap.dedent("""\
class a():
def a(): return a(
aaaaaaaaaa=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
""")
expected_formatted_code = textwrap.dedent("""\
class a():
def a():
return a(
aaaaaaaaaa=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Expand Down Expand Up @@ -501,6 +505,7 @@ class Demo:
"""
Demo docs
"""
def foo(self):
"""
foo docs
Expand Down Expand Up @@ -602,6 +607,7 @@ def __init__(self, title: Optional[str], diffs: Collection[BinaryDiff] = (), cha
""")
expected_formatted_code = textwrap.dedent("""\
class _():
def __init__(
self,
title: Optional[str],
Expand Down
2 changes: 2 additions & 0 deletions yapftests/reformatter_python3_test.py
Expand Up @@ -238,6 +238,7 @@ def testAsyncFunctionsNested(self):
return
code = textwrap.dedent("""\
async def outer():
async def inner():
pass
""")
Expand Down Expand Up @@ -365,6 +366,7 @@ def foo(self):
"""
expected_formatted_code = """\
class Foo:
def foo(self):
foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
'foo': 'foo',
Expand Down
2 changes: 2 additions & 0 deletions yapftests/yapf_test.py
Expand Up @@ -735,12 +735,14 @@ def testDisableWholeDataStructure(self):
def testDisableButAdjustIndentations(self):
unformatted_code = textwrap.dedent("""\
class SplitPenaltyTest(unittest.TestCase):
def testUnbreakable(self):
self._CheckPenalties(tree, [
]) # yapf: disable
""")
expected_formatted_code = textwrap.dedent("""\
class SplitPenaltyTest(unittest.TestCase):
def testUnbreakable(self):
self._CheckPenalties(tree, [
]) # yapf: disable
Expand Down

2 comments on commit 5fda04e

@SAV1983
Copy link

Choose a reason for hiding this comment

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

Y

@SAV1983
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.