ruff: Allow newlines after block open (or not)
Hi, I’m opening this issue per a suggestion from @MichaReiser, based on this closed issue: https://github.com/astral-sh/ruff/issues/8893.
The formatting question is whether Ruff should automatically remove a blank line after a block definition:
def some_func():
some_more_code()
to
def some_func():
some_more_code()
The alternative is “allow zero or one” newlines, i.e. not to remove the newline if it exists, but also not to automatically add one. This is the approach that Black chose in their 2024 preview style. Full Black discussion here: https://github.com/psf/black/issues/4043.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 57
- Comments: 30 (8 by maintainers)
From the perspective of someone with dyslexia; this really makes code harder to read - I need whitespace around the block to help parse what’s in it.
Also checking in to say the same as above, I think this is our last remaining “issue” with ruff.
We are also in the blank line after all blocks of code camp (at least the option as does black). Excited to see this happen and move all of our stuff to ruff!
Was excited to start moving all of our codebases to ruff but discovered this issue with the blank line after func definitions. I second that it makes code much easier to read.
Hopefully this gets implemented in the near future! For now back to black.
Hi, what’s the status on this? If I’m not mistaken, Black already made this stable in 2024.
I see that this was discussed for https://github.com/astral-sh/ruff/issues/8678 in #8893. Then it was briefly mentioned in the plans for the 2025 style guide in https://github.com/astral-sh/ruff/issues/13371#issuecomment-2498529767, but it wasn’t planned.
We’ve switched from black to ruff and loving it, but this issue prevents us from formatting Django migrations without the unnecessary noise of removing empty newlines after the
classheader that Black leaves as-is.Hi, yes please add an option to preserve up to 1 empty line.
As empty lines used for separating/grouping lines, the first line(s) stay closer to block head but not the remaining parts feel like it’s part of the block head (since empty line inside block content is inevitable), e.g.
so I prefer to read as
Personally I always insert an empty line at the beginning of the block, except 2 scenarios:
For me it’s mainly about the diff that is created. Working in a big monorepo, it’s always easier for us toolies to roll out changes if we don’t need to touch the other devs code. Having read that
ruffwould be a “drop-in” replacement forblack, my expectation was that I would see no diff at all.Having said that and after looking at the known deviations I can’t say that I don’t agree with ruff’s design decisions though…
I think (and know in my case) that it applies to all blocks; I have a lot of code that has blank lines after conditionals, loops, and other constructs.
One more datapoint: Django generates migrations/*.py files that look like:
Current black-25.1.0 has no opinion on this (does not add nor remove the line break), but ruff-0.9.9 removes it.
We recently switched from black to ruff format at work and this issue caused many thousands of lines of diff, very little of which was an improvement and a lot of which imo hurt readability. This is currently a top 3 upvoted ruff formatter issue, so fingers crossed
I think the ones where it most often feels bad to me involve functions with type annotations and no docstring. There are some other cases involving dense if statements, especially nested ones or ones with comments. And a longer tail of more debatable cases, e.g. where it improves symmetry. A few not particularly cherry picked examples here: https://play.ruff.rs/bd268928-cf4e-458a-b41d-a1f163cb6347?secondary=Format
We mainly would like to have blank lines after class headers. Often after function and method headers. Only very rarely for conditional or loop blocks (neglectable).
Also, as mentioned in https://github.com/astral-sh/ruff/issues/9745#issuecomment-2662810468 and https://github.com/astral-sh/ruff/issues/9745#issuecomment-2695173158, Django migrations generate classes with a newline after the
classheader. Applying it for classes would allow for switching from black to ruff with less noisy diffs.Like others here, we were really excited to implement Ruff in our project due to its impressive performance, but this issue is kind of stopping us from making the move. Hopefully, this change gets implemented soon.
Second this change. I only adopted black as a formatter after they implemented this change as I find it very difficult to read code without the extra newline. This is especially so when the start of the block is very long, and there’s a comment right underneath the block.
For example:
I find this to be much easier to read:
For formatters in other languages that don’t support this extra new line, I use a hack to force a new line:
Since the Pylsp formatter and black already allow an extra new line, I don’t bother doing this hack for my Python code. It would be great to have Ruff support this.