Skip to content

Format: Blank lines before classes minor inconsistency #8215

@Paul-Aime

Description

@Paul-Aime

ruff format generally adds two blank lines between a class and a previous commented line.
But it does not if the class is the first line of code of the file.

Not a bug and only a minor inconsistency, but also black does always add two blank lines, so just documenting the behavior here.

Reproducible example

Consider following file ruff_inconsistency.py:

# a = 1  # uncomment to change behavior

# comment

class A:
    def foo(self):
        pass

# comment

class B:
    def bar(self):
        pass

will format into:

# a = 1  # uncomment to change behavior

# comment

class A:
    def foo(self):
        pass


# comment


class B:
    def bar(self):
        pass

We can see a single blank line before class A: but two blank lines before class B:.

As a note, black does not exhibit that behavior, and consistently add two blank lines:

$ diff <(black -q - < ruff_inconsistency.py) <(ruff format -q - < ruff_inconsistency.py)
5d4
<

Versions

$ ruff --version
ruff 0.1.2

$ black --version
black, 23.10.0 (compiled: no)
Python (CPython) 3.12.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions