yapf 0.28.0 now removes blank line between class docstring and first function

Created on 25 Jul 2019  路  9Comments  路  Source: google/yapf

Given this code:

class Foo:
    """Cool class"""

    def bar(self):
        """Cool function"""
        pass

yapf 0.28.0 now removes the line after """Cool class""", which makes pydocstyle complain with:

D204: 1 blank line required after class docstring

Yapf 0.27.0 keeps this blank line.

Most helpful comment

Okay, so just to make sure we're on the same page. In PEP8 style, there should be a blank line between a class's docstring and the first nested function/class (the text you quoted has "one-line" along with "multi-line"). There should not be a blank line if there isn't a docstring. This hold similarly for functions.

All 9 comments

Which style are you using? With --style pep8 it removes the blank line. There was a recent patch to change how the style is processed (9e3cb7196a). Could that be causing this?

Yes, it's using the default style pep8:

$ echo '
class Foo:
    """Cool class"""

    def bar(self):
        """Cool function"""
        pass
' | yapf

Though it seems to differ from the actual pep8 recommendation (yapf up to version 0.27.0 was correctly preserving the blank line).

If I set BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF to True, several of the PEP8 tests fail. E.g.:

Diff:
--- actual
+++ expected
@@ -1,5 +1,4 @@
 class Foo:
-
     def foo(self):
         foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
             'foo': 'foo',

Is having the blank line in this situation correct?

In this test case there is no docstring. I think the blank line should only be here when there is a docstring.

Would that also apply to nested functions?

def _():
  """some docstring here"""

  def nested():
    pass

In this case I think PEP8 says there should not be a blank line.

Actually reading https://www.python.org/dev/peps/pep-0008/#blank-lines it seems that even without docstring, methods in a class should be surrounded by a single blank line, so there probably _should_ be a line between class Foo: and def foo(self):.

https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings

Insert a blank line after all docstrings (one-line or multi-line) that document a class -- generally speaking, the class's methods are separated from each other by a single blank line, and the docstring needs to be offset from the first method by a blank line.

Okay, so just to make sure we're on the same page. In PEP8 style, there should be a blank line between a class's docstring and the first nested function/class (the text you quoted has "one-line" along with "multi-line"). There should not be a blank line if there isn't a docstring. This hold similarly for functions.

PEP8 says nothing regarding nested functions, and indeed pydocstyle accepts either zero or one (but not more) blank lines between a function docstring and a nested function.

Was this page helpful?
0 / 5 - 0 ratings