-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
formatter removes blank line after class definition #8566
Comments
Do you know what Black version you're using? The behavior is identical for me in the playground: https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACnAGxdAD2IimZxl1N_WlXnON2nzNX9HFvHyHbXdX0Ms6iKT6MFYYQATMUD1OePdwlX142nVYoknzNqjCZuF1C-2qRxjAe1b_PNzGhgl2Uikz5qLOibGl5-HYJNlgbh2oqOzRkM6DJB73sLqD6q1cogjgBuZBfc0jIxPgABiAGoAQAABTDLhbHEZ_sCAAAAAARZWg== |
same for: class Foo:
# bar
def __init__(self):
pass Additionally I would like to have a flag in ruff which prevents that class Foo:
def __init__(self):
pass get converted to: class Foo:
def __init__(self):
pass |
Our current behavior is identical to Black's though in all of the above cases -- see the playground link I shared above. |
OK, then see this as feature request to change it via a config option. |
Realistically, I think we're unlikely to implement a setting for this given that it's so specific. We also enforce a blank line if you use a docstring for your classes which should mitigate this, but I'm gonna close for now in the interest of keeping the issue queue manageable. |
This behavior changed between |
This formatting is one of multiple reasons I don't use black - the maintainers also have strong opinions and you cannot argue with them. I have the hope that ruff is more user friendly and configurable. |
As long as ruff follows what black does, the same problem will exist. Soon it's 2024, and black will do some changes to their format again. There's a separate issue for tracking that here: #8678. |
I'm using Input fileclass MyClass:
def __init__(self):
pass blackclass MyClass:
def __init__(self):
pass ruffclass MyClass:
def __init__(self):
pass Something interesting to note is that black will leave the blank line after class statement if it is there, but won't insert one if it's not already there. This means if you take my input file and run black then ruff, only ruff will make changes to the file. However, if you run ruff then black, black will not change the file - it won't revert the ruff changes. It will accept the ruff formatting. This means there's a subtle difference in behavior that could be made more consistent between the two tools:
What's the stance on conformity with black? It would help me a lot if it was a true drop-in like-for-like. Side note: Thanks for an AMAZING project! I appreciate how complicated it is to manage everyones expectations. |
Hi @jongracecox Thanks for the excellent write-up. Yes, Black 24 or newer allows an optional blank line at the start of a block. We intentionally didn't implement this design change (yet?) because it can lead to unintentionally blank lines at the top of blocks after moving or deleting code.
I'm glad that you pointed this out because it made me realize that the known deviations on our website don't mention this deviation yet because I only updated the
Thank you |
Using Any idea how to make ruff output equal to black ? black:class MyClass(object):
def test(self):
pass
for i in range(1):
pass ruff:class MyClass(object):
def test(self):
pass
for i in range(1):
pass |
$ black --diff foo.py All done! ✨ 🍰 ✨ 1 file would be left unchanged. $ ruff format --diff foo.py --- foo.py +++ foo.py @@ -1,5 +1,4 @@ class Foo: - bar = 1 def __init__(self): $ cat foo.py class Foo: bar = 1 def __init__(self): pass
→ ruff should not remove the blank line
The text was updated successfully, but these errors were encountered: