Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format: Blank lines before classes minor inconsistency #8215

Closed
Paul-Aime opened this issue Oct 25, 2023 · 1 comment · Fixed by #8224
Closed

Format: Blank lines before classes minor inconsistency #8215

Paul-Aime opened this issue Oct 25, 2023 · 1 comment · Fixed by #8224
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@Paul-Aime
Copy link

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
@charliermarsh charliermarsh added bug Something isn't working formatter Related to the formatter labels Oct 25, 2023
@charliermarsh charliermarsh added this to the Formatter: Stable milestone Oct 25, 2023
@charliermarsh
Copy link
Member

Thanks!

@charliermarsh charliermarsh self-assigned this Oct 25, 2023
charliermarsh added a commit that referenced this issue Oct 26, 2023
## Summary

Given:

```python
# comment

class A:
    def foo(self):
        pass
```

We need to insert an additional newline between `# comment` and `class
A`. We were missing this handling for the case in which `# comment` is a
leading comment on `class A`, as opposed to a trailing comment of some
preceding statement.

In practice, I think this only applies to the specific case in which a
class or function is the first statement in a module, and there's a
single empty line between a leading comment and that class or function.
If there are no empty lines, then the comment "sticks" to the
definition; if there are two or more, then `leading_comments` will
truncate appropriately. If the class or function is nested, then we only
need one empty line anyway.

Closes #8215.

## Test Plan

No change in similarity.

Before:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

After:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1648 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants