Allow up to two empty lines after top-level imports (#6777)

## Summary

For imports, we enforce that there's _at least_ one empty line after an
import (assuming the next statement is _not_ an import), but allow up to
two at the module level.

Closes https://github.com/astral-sh/ruff/issues/6760.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-22 12:27:40 -04:00 committed by GitHub
parent 558b56f8a8
commit cc278c24e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 176 additions and 1 deletions

View file

@ -192,7 +192,19 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
}
} else if is_import_definition(preceding) && !is_import_definition(following) {
empty_line().fmt(f)?;
// Enforce _at least_ one empty line after an import statement (but allow up to
// two at the top-level).
match self.kind {
SuiteKind::TopLevel => {
match lines_after_ignoring_trivia(preceding.end(), source) {
0 | 1 | 2 => empty_line().fmt(f)?,
_ => write!(f, [empty_line(), empty_line()])?,
}
}
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
empty_line().fmt(f)?;
}
}
} else if is_compound_statement(preceding) {
// 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.