mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 03:15:44 +00:00
Truncate to one empty line in stub files (#7558)
## Summary This PR modifies a variety of sites in which we insert up to two empty lines to instead truncate to at most one empty line in stub files. We already enforce this in _some_ places, but not all. ## Test Plan `cargo test` No changes in similarity (as expected, since this only impacts unformatted `.pyi` files). Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 323 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 323 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 |
This commit is contained in:
parent
c3774e1255
commit
d7508af48d
4 changed files with 506 additions and 6 deletions
161
crates/ruff_python_formatter/resources/test/fixtures/ruff/newlines.pyi
vendored
Normal file
161
crates/ruff_python_formatter/resources/test/fixtures/ruff/newlines.pyi
vendored
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
###
|
||||||
|
# Blank lines around functions
|
||||||
|
###
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if True:
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
###
|
||||||
|
# Blank lines around imports.
|
||||||
|
###
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x # comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f(): pass # comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
|
@ -308,7 +308,14 @@ impl Format<PyFormatContext<'_>> for FormatEmptyLines {
|
||||||
NodeLevel::TopLevel => match self.lines {
|
NodeLevel::TopLevel => match self.lines {
|
||||||
0 | 1 => write!(f, [hard_line_break()]),
|
0 | 1 => write!(f, [hard_line_break()]),
|
||||||
2 => write!(f, [empty_line()]),
|
2 => write!(f, [empty_line()]),
|
||||||
_ => write!(f, [empty_line(), empty_line()]),
|
_ => match f.options().source_type() {
|
||||||
|
PySourceType::Stub => {
|
||||||
|
write!(f, [empty_line()])
|
||||||
|
}
|
||||||
|
PySourceType::Python | PySourceType::Ipynb => {
|
||||||
|
write!(f, [empty_line(), empty_line()])
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
NodeLevel::CompoundStatement => match self.lines {
|
NodeLevel::CompoundStatement => match self.lines {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
|
||||||
use ruff_python_ast::helpers::is_compound_statement;
|
use ruff_python_ast::helpers::is_compound_statement;
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, Stmt, Suite};
|
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, PySourceType, Stmt, Suite};
|
||||||
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
|
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
|
@ -192,7 +192,14 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
SuiteKind::TopLevel => {
|
SuiteKind::TopLevel => {
|
||||||
match lines_after_ignoring_trivia(preceding.end(), source) {
|
match lines_after_ignoring_trivia(preceding.end(), source) {
|
||||||
0..=2 => empty_line().fmt(f)?,
|
0..=2 => empty_line().fmt(f)?,
|
||||||
_ => write!(f, [empty_line(), empty_line()])?,
|
_ => match source_type {
|
||||||
|
PySourceType::Stub => {
|
||||||
|
empty_line().fmt(f)?;
|
||||||
|
}
|
||||||
|
PySourceType::Python | PySourceType::Ipynb => {
|
||||||
|
write!(f, [empty_line(), empty_line()])?;
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
|
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
|
||||||
|
@ -225,8 +232,15 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
match lines_before(start, source) {
|
match lines_before(start, source) {
|
||||||
0 | 1 => hard_line_break().fmt(f)?,
|
0 | 1 => hard_line_break().fmt(f)?,
|
||||||
2 => empty_line().fmt(f)?,
|
2 => empty_line().fmt(f)?,
|
||||||
3.. => match self.kind {
|
_ => match self.kind {
|
||||||
SuiteKind::TopLevel => write!(f, [empty_line(), empty_line()])?,
|
SuiteKind::TopLevel => match source_type {
|
||||||
|
PySourceType::Stub => {
|
||||||
|
empty_line().fmt(f)?;
|
||||||
|
}
|
||||||
|
PySourceType::Python | PySourceType::Ipynb => {
|
||||||
|
write!(f, [empty_line(), empty_line()])?;
|
||||||
|
}
|
||||||
|
},
|
||||||
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
|
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
|
||||||
empty_line().fmt(f)?;
|
empty_line().fmt(f)?;
|
||||||
}
|
}
|
||||||
|
@ -280,7 +294,14 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
NodeLevel::TopLevel => match count_lines(end) {
|
NodeLevel::TopLevel => match count_lines(end) {
|
||||||
0 | 1 => hard_line_break().fmt(f)?,
|
0 | 1 => hard_line_break().fmt(f)?,
|
||||||
2 => empty_line().fmt(f)?,
|
2 => empty_line().fmt(f)?,
|
||||||
_ => write!(f, [empty_line(), empty_line()])?,
|
_ => match source_type {
|
||||||
|
PySourceType::Stub => {
|
||||||
|
empty_line().fmt(f)?;
|
||||||
|
}
|
||||||
|
PySourceType::Python | PySourceType::Ipynb => {
|
||||||
|
write!(f, [empty_line(), empty_line()])?;
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
NodeLevel::CompoundStatement => match count_lines(end) {
|
NodeLevel::CompoundStatement => match count_lines(end) {
|
||||||
0 | 1 => hard_line_break().fmt(f)?,
|
0 | 1 => hard_line_break().fmt(f)?,
|
||||||
|
|
|
@ -0,0 +1,311 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/newlines.pyi
|
||||||
|
---
|
||||||
|
## Input
|
||||||
|
```py
|
||||||
|
###
|
||||||
|
# Blank lines around functions
|
||||||
|
###
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if True:
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
###
|
||||||
|
# Blank lines around imports.
|
||||||
|
###
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x # comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
|
||||||
|
def f(): pass # comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
```
|
||||||
|
|
||||||
|
## Output
|
||||||
|
```py
|
||||||
|
###
|
||||||
|
# Blank lines around functions
|
||||||
|
###
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
# comment
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
###
|
||||||
|
# Blank lines around imports.
|
||||||
|
###
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
import x # comment
|
||||||
|
# comment
|
||||||
|
|
||||||
|
import y
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass # comment
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# comment
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue