Ignore blank lines between comments when counting newlines-after-imports (#7607)

## Summary

Given:

```python
# -*- coding: utf-8 -*-
import random

# Defaults for arguments are defined here
# args.threshold = None;


logger = logging.getLogger("FastProject")
```

We want to count the number of newlines after `import random`, to ensure
that there's _at least one_, but up to two.

Previously, we used the end range of the statement (then skipped
trivia); instead, we need to use the end of the _last comment_. This is
similar to #7556.

Closes https://github.com/astral-sh/ruff/issues/7604.
This commit is contained in:
Charlie Marsh 2023-09-22 13:49:39 -04:00 committed by GitHub
parent 8bfe9bda41
commit 5174e8c926
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View file

@ -56,3 +56,12 @@ def func():
x = 1
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os
# Defaults for arguments are defined here
# args.threshold = None;
logger = logging.getLogger("FastProject")

View file

@ -190,7 +190,12 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
// a leading comment.
match self.kind {
SuiteKind::TopLevel => {
match lines_after_ignoring_trivia(preceding.end(), source) {
let end = if let Some(last_trailing) = preceding_comments.trailing.last() {
last_trailing.end()
} else {
preceding.end()
};
match lines_after(end, source) {
0..=2 => empty_line().fmt(f)?,
_ => match source_type {
PySourceType::Stub => {

View file

@ -62,6 +62,15 @@ def func():
x = 1
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os
# Defaults for arguments are defined here
# args.threshold = None;
logger = logging.getLogger("FastProject")
```
## Output
@ -130,6 +139,16 @@ def func():
import sys
x = 1
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
import os
# Defaults for arguments are defined here
# args.threshold = None;
logger = logging.getLogger("FastProject")
```