Black: One space between class definition and the first inner method

Created on 24 Nov 2018  路  8Comments  路  Source: psf/black

Can Black have a rule to not remove the whitespace at line 2 from here:

class C:

    def __init__(self):
        pass

so that it will keep the blank line between class definition and any first inner entity (as it does already with the docstrings and assignment attributes)?

It's extremely ugly to have that whitespace consistent in the entire codebase and just in this particular situation, where you don't have anything to add between class definition and the first inner method (which usually is the __init__ one), to end up with "glued" headers of class and method definition.

Now if I'm formatting this:

class C:

    def __init__(self):
        pass
    def func(self):
        pass

I get this:

class C:
    def __init__(self):
        pass

    def func(self):
        pass

So I expect the rule of _one empty line before and after class methods_ to be consistent here. Everyone likes to have symmetry in their code.

bug empty lines

Most helpful comment

What a coincidence, I used black for the first time a few minutes ago and I had the same remark. I didn't bother opening an issue though because I thought it has been suggested before but refused for some reason?

PEP8 states that:

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

So I guess this applies for the first method too, including __init__().

All 8 comments

What a coincidence, I used black for the first time a few minutes ago and I had the same remark. I didn't bother opening an issue though because I thought it has been suggested before but refused for some reason?

PEP8 states that:

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

So I guess this applies for the first method too, including __init__().

Just tested how black would format Robot Framework source code and this was the biggest problem. I found it important to surround methods with an empty line and think it should be done consistently. I understand that the motivation behind black is remove the need to discuss about different formatting alternatives, but in this particular I think black just behaves wrong. As already pointed above, PEP-8 explicitly states that

"Method definitions inside a class are surrounded by a single blank line."

In my opinion the PEP-8 compatible behavior should be the default. There could be a command line option to omit the empty line to avoid changes with existing blackened code bases. Having an option to preserve/add it would be the minimum.

I think black鈥檚 behaviour make sense for classes that start with a docstring or comment. In this case, the empty line is wanted:

```python
class C:
"""Abstraction for B to do spam."""

def __init__(self):
    ...

````

@merwok I totally agree there should be an empty line after the docstring. I think the docstring PEP also explicitly says so. This issue is about black _removing_ the empty line if there's no docstring, even though that's against PEP-8. Without the docstring, black would format you example like this:

class C:
    def __init__(self):
        ...

This is interesting. While your reading of PEP 8 is correct to the letter, I'm not quite sure if it was intended by the PEP 8 authors. Case in point: the PEP also says the following:

Surround top-level function and class definitions with two blank lines.

Following your logic, if a Python file starts with a function or a class definition, we would have to put two blank lines before it, no?

I'd like to understand your need for this blank line better. What purpose does it serve? There's clear delineation due to the increased indentation. In fact, I'm leaning towards implementing #450 so that we are consistent in that regard.

if a Python file starts with a function or a class definition, we would have to put two blank lines before it

pep8 or flake8 used to require that and it was annoying!

FWIW my take on the question here:

class CoolRegularClass:
    """Nice docstring.

    More info.
    Cool.
    """

    def __init__(self):
        # nice to have one blank after the docstring
        ...


class SomeInternalClass:
    def __init__(self):
        # no docstring, no needless blank line
        ...

In other words: the class statement is directly followed by the first interesting thing (docstring, attribute, method), then usual blank lines conventions applies.

Some reasons why I prefer an empty line before the first method in a class:

  • There are empty lines above other methods as well. I like consistency in this regard.
  • PEP-8 says so. There are likely lot of projects following that rule and Black forcing other style causes unnecessary code churn (or just prevents taking the tool into use).
  • I find it odd if docstrings or class attributes change this behaviour.
  • For my eye it just looks better.

I guess this is something where opinions differ and in my opinion stuff like this should be configurable. I know Black tries to be uncompromising, but there's already similar --skip-string-normalization option. It would be awesome if projects could have their own black.toml for options like this. Black could set the overall rules and let project decide on nuances like this. Black doing all formatting automatically would give you its normal benefits within a project. Two projects not having exactly same styles shouldn't be a big problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JelleZijlstra picture JelleZijlstra  路  3Comments

asottile picture asottile  路  3Comments

decibyte picture decibyte  路  3Comments

dgnsrekt picture dgnsrekt  路  3Comments

testvinder picture testvinder  路  3Comments