mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 13:05:06 +00:00
Add empty line after import
block (#6200)
## Summary Ensures that, given: ```python import os x = 1 ``` We format like: ```python import os x = 1 ```
This commit is contained in:
parent
cb34e6d322
commit
7eb2ba47cc
3 changed files with 24 additions and 19 deletions
|
@ -58,17 +58,13 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
// First entry has never any separator, doesn't matter which one we take;
|
// First entry has never any separator, doesn't matter which one we take.
|
||||||
joiner.entry(first, &first.format());
|
joiner.entry(first, &first.format());
|
||||||
|
|
||||||
let mut last = first;
|
let mut last = first;
|
||||||
let mut is_last_function_or_class_definition = is_class_or_function_definition(first);
|
|
||||||
|
|
||||||
for statement in iter {
|
for statement in iter {
|
||||||
let is_current_function_or_class_definition =
|
if is_class_or_function_definition(last) || is_class_or_function_definition(statement) {
|
||||||
is_class_or_function_definition(statement);
|
|
||||||
|
|
||||||
if is_last_function_or_class_definition || is_current_function_or_class_definition {
|
|
||||||
match self.level {
|
match self.level {
|
||||||
SuiteLevel::TopLevel => {
|
SuiteLevel::TopLevel => {
|
||||||
joiner.entry_with_separator(
|
joiner.entry_with_separator(
|
||||||
|
@ -81,6 +77,8 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
joiner.entry_with_separator(&empty_line(), &statement.format(), statement);
|
joiner.entry_with_separator(&empty_line(), &statement.format(), statement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if is_import_definition(last) && !is_import_definition(statement) {
|
||||||
|
joiner.entry_with_separator(&empty_line(), &statement.format(), statement);
|
||||||
} else if is_compound_statement(last) {
|
} else if is_compound_statement(last) {
|
||||||
// Handles the case where a body has trailing comments. The issue is that RustPython does not include
|
// Handles the case where a body has trailing comments. The issue is that RustPython does not include
|
||||||
// the comments in the range of the suite. This means, the body ends right after the last statement in the body.
|
// the comments in the range of the suite. This means, the body ends right after the last statement in the body.
|
||||||
|
@ -124,7 +122,6 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
joiner.entry(statement, &statement.format());
|
joiner.entry(statement, &statement.format());
|
||||||
}
|
}
|
||||||
|
|
||||||
is_last_function_or_class_definition = is_current_function_or_class_definition;
|
|
||||||
last = statement;
|
last = statement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +140,10 @@ const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fn is_import_definition(stmt: &Stmt) -> bool {
|
||||||
|
matches!(stmt, Stmt::Import(_) | Stmt::ImportFrom(_))
|
||||||
|
}
|
||||||
|
|
||||||
impl FormatRuleWithOptions<Suite, PyFormatContext<'_>> for FormatSuite {
|
impl FormatRuleWithOptions<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
type Options = SuiteLevel;
|
type Options = SuiteLevel;
|
||||||
|
|
||||||
|
|
|
@ -198,20 +198,21 @@ d={'a':1,
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -6,10 +6,9 @@
|
@@ -6,10 +6,10 @@
|
||||||
|
|
||||||
from library import some_connection, some_decorator
|
from library import some_connection, some_decorator
|
||||||
# fmt: off
|
# fmt: off
|
||||||
-from third_party import (X,
|
-from third_party import (X,
|
||||||
- Y, Z)
|
- Y, Z)
|
||||||
+from third_party import X, Y, Z
|
+from third_party import X, Y, Z
|
||||||
|
+
|
||||||
# fmt: on
|
# fmt: on
|
||||||
-f"trigger 3.6 mode"
|
-f"trigger 3.6 mode"
|
||||||
+f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
+f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
||||||
# Comment 1
|
# Comment 1
|
||||||
|
|
||||||
# Comment 2
|
# Comment 2
|
||||||
@@ -17,26 +16,40 @@
|
@@ -17,26 +17,40 @@
|
||||||
|
|
||||||
# fmt: off
|
# fmt: off
|
||||||
def func_no_args():
|
def func_no_args():
|
||||||
|
@ -270,7 +271,7 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
|
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
|
||||||
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
|
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2)))
|
||||||
@@ -63,15 +76,15 @@
|
@@ -63,15 +77,15 @@
|
||||||
|
|
||||||
something = {
|
something = {
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -289,13 +290,14 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
goes + here,
|
goes + here,
|
||||||
andhere,
|
andhere,
|
||||||
@@ -80,38 +93,35 @@
|
@@ -80,38 +94,36 @@
|
||||||
|
|
||||||
def import_as_names():
|
def import_as_names():
|
||||||
# fmt: off
|
# fmt: off
|
||||||
- from hello import a, b
|
- from hello import a, b
|
||||||
- 'unformatted'
|
- 'unformatted'
|
||||||
+ from hello import a, b
|
+ from hello import a, b
|
||||||
|
+
|
||||||
+ "unformatted"
|
+ "unformatted"
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
@ -338,7 +340,7 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
||||||
@@ -132,10 +142,10 @@
|
@@ -132,10 +144,10 @@
|
||||||
"""Another known limitation."""
|
"""Another known limitation."""
|
||||||
# fmt: on
|
# fmt: on
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -353,7 +355,7 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
# fmt: off
|
# fmt: off
|
||||||
# ...but comments still get reformatted even though they should not be
|
# ...but comments still get reformatted even though they should not be
|
||||||
@@ -153,9 +163,7 @@
|
@@ -153,9 +165,7 @@
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -364,7 +366,7 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
_type_comment_re = re.compile(
|
_type_comment_re = re.compile(
|
||||||
r"""
|
r"""
|
||||||
@@ -178,7 +186,7 @@
|
@@ -178,7 +188,7 @@
|
||||||
$
|
$
|
||||||
""",
|
""",
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -373,7 +375,7 @@ d={'a':1,
|
||||||
# fmt: on
|
# fmt: on
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -216,8 +224,7 @@
|
@@ -216,8 +226,7 @@
|
||||||
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
|
xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5,
|
||||||
)
|
)
|
||||||
# fmt: off
|
# fmt: off
|
||||||
|
@ -399,6 +401,7 @@ from third_party import X, Y, Z
|
||||||
from library import some_connection, some_decorator
|
from library import some_connection, some_decorator
|
||||||
# fmt: off
|
# fmt: off
|
||||||
from third_party import X, Y, Z
|
from third_party import X, Y, Z
|
||||||
|
|
||||||
# fmt: on
|
# fmt: on
|
||||||
f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
||||||
# Comment 1
|
# Comment 1
|
||||||
|
@ -486,6 +489,7 @@ def subscriptlist():
|
||||||
def import_as_names():
|
def import_as_names():
|
||||||
# fmt: off
|
# fmt: off
|
||||||
from hello import a, b
|
from hello import a, b
|
||||||
|
|
||||||
"unformatted"
|
"unformatted"
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
|
@ -107,17 +107,16 @@ def __await__(): return (yield)
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -5,8 +5,7 @@
|
@@ -6,7 +6,7 @@
|
||||||
from third_party import X, Y, Z
|
|
||||||
|
|
||||||
from library import some_connection, some_decorator
|
from library import some_connection, some_decorator
|
||||||
-
|
|
||||||
-f"trigger 3.6 mode"
|
-f"trigger 3.6 mode"
|
||||||
+f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
+f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
||||||
|
|
||||||
|
|
||||||
def func_no_args():
|
def func_no_args():
|
||||||
@@ -65,18 +64,14 @@
|
@@ -65,18 +65,14 @@
|
||||||
|
|
||||||
def spaces2(result=_core.Value(None)):
|
def spaces2(result=_core.Value(None)):
|
||||||
assert fut is self._read_fut, (fut, self._read_fut)
|
assert fut is self._read_fut, (fut, self._read_fut)
|
||||||
|
@ -153,6 +152,7 @@ import sys
|
||||||
from third_party import X, Y, Z
|
from third_party import X, Y, Z
|
||||||
|
|
||||||
from library import some_connection, some_decorator
|
from library import some_connection, some_decorator
|
||||||
|
|
||||||
f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
f"NOT_YET_IMPLEMENTED_ExprJoinedStr"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue