diff --git a/CHANGELOG b/CHANGELOG index a35ce689a..4cecbd3e2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py index 3d2710923..a1e6940bf 100644 --- a/yapf/yapflib/style.py +++ b/yapf/yapflib/style.py @@ -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, @@ -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 @@ -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 diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py index a67e4c47b..8dce567dd 100644 --- a/yapftests/reformatter_basic_test.py +++ b/yapftests/reformatter_basic_test.py @@ -1982,6 +1982,7 @@ def testMultilineDictionaryKeys(self): def testStableDictionaryFormatting(self): code = textwrap.dedent("""\ class A(object): + def method(self): filters = { 'expressions': [{ diff --git a/yapftests/reformatter_pep8_test.py b/yapftests/reformatter_pep8_test.py index a5301f1fc..e1202c2a2 100644 --- a/yapftests/reformatter_pep8_test.py +++ b/yapftests/reformatter_pep8_test.py @@ -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): @@ -77,6 +77,7 @@ def is_running(self): ''') expected_formatted_code = textwrap.dedent('''\ class TestClass: + def __init__(self): self.running = False @@ -174,6 +175,7 @@ def g(): """) expected_formatted_code = textwrap.dedent("""\ def f(): + def g(): while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and xxxxxxxxxxxxxxxxxxxx( @@ -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 @@ -501,6 +505,7 @@ class Demo: """ Demo docs """ + def foo(self): """ foo docs @@ -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], diff --git a/yapftests/reformatter_python3_test.py b/yapftests/reformatter_python3_test.py index d06e40623..ae557552c 100644 --- a/yapftests/reformatter_python3_test.py +++ b/yapftests/reformatter_python3_test.py @@ -238,6 +238,7 @@ def testAsyncFunctionsNested(self): return code = textwrap.dedent("""\ async def outer(): + async def inner(): pass """) @@ -365,6 +366,7 @@ def foo(self): """ expected_formatted_code = """\ class Foo: + def foo(self): foofoofoofoofoofoofoofoo('foofoofoofoofoo', { 'foo': 'foo', diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py index dc0d0a5e6..4e062cf36 100644 --- a/yapftests/yapf_test.py +++ b/yapftests/yapf_test.py @@ -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