Skip to content

Commit

Permalink
Limit isort.lines-after-imports to 1 for stub files
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Feb 13, 2024
1 parent d96a0db commit d00bb85
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import Any

from requests import Session

from my_first_party import my_first_party_object

from . import my_local_folder_object



class Thing(object):
name: str
def __init__(self, name: str):
self.name = name
10 changes: 9 additions & 1 deletion crates/ruff_linter/src/rules/isort/mod.rs
Expand Up @@ -108,7 +108,14 @@ pub(crate) fn format_imports(
output.push_str(block_output.as_str());
}

let lines_after_imports = settings.lines_after_imports;
let lines_after_imports = if source_type.is_stub() {
// Limit the number of lines after imports in stub files to 1 to be compatible with the formatter similar to `isort`
// when using the profile `black`.
settings.lines_after_imports.min(1)
} else {
settings.lines_after_imports
};

match trailer {
None => {}
Some(Trailer::Sibling) => {
Expand Down Expand Up @@ -978,6 +985,7 @@ mod tests {
}

#[test_case(Path::new("lines_after_imports_nothing_after.py"))]
#[test_case(Path::new("lines_after_imports.pyi"))]
#[test_case(Path::new("lines_after_imports_func_after.py"))]
#[test_case(Path::new("lines_after_imports_class_after.py"))]
fn lines_after_imports(path: &Path) -> Result<()> {
Expand Down
@@ -0,0 +1,41 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
lines_after_imports.pyi:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from __future__ import annotations
2 | |
3 | | from typing import Any
4 | |
5 | | from requests import Session
6 | |
7 | | from my_first_party import my_first_party_object
8 | |
9 | | from . import my_local_folder_object
10 | |
11 | |
12 | |
13 | | class Thing(object):
| |_^ I001
14 | name: str
15 | def __init__(self, name: str):
|
= help: Organize imports

Safe fix
2 2 |
3 3 | from typing import Any
4 4 |
5 |-from requests import Session
6 |-
7 5 | from my_first_party import my_first_party_object
6 |+from requests import Session
8 7 |
9 8 | from . import my_local_folder_object
10 |-
11 |-
12 9 |
13 10 | class Thing(object):
14 11 | name: str


3 changes: 3 additions & 0 deletions crates/ruff_workspace/src/options.rs
Expand Up @@ -2049,6 +2049,9 @@ pub struct IsortOptions {
/// The number of blank lines to place after imports.
/// Use `-1` for automatic determination.
///
/// The number of blank lines in typing stub files with `.pyi` extension is limited to at most one in accordance to
/// the stub file style recommendations ([source](https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#stub-file-coding-style)).
///
/// When using the formatter, only the values `-1`, `1`, and `2` are compatible because
/// it enforces at least one empty and at most two empty lines after imports.
#[option(
Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d00bb85

Please sign in to comment.