Skip to content

Commit

Permalink
Fix isort no-lines-before preceded by an empty section (#3139)
Browse files Browse the repository at this point in the history
Fix isort no-lines-before preceded by an empty section

Fix #3138.
  • Loading branch information
bluetech committed Feb 22, 2023
1 parent 17ab71f commit ba61bb6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
@@ -0,0 +1,3 @@
from __future__ import annotations
from typing import Any
from . import my_local_folder_object
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/isort/categorize.rs
Expand Up @@ -6,12 +6,13 @@ use log::debug;
use ruff_python::sys::KNOWN_STANDARD_LIBRARY;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;

use super::types::{ImportBlock, Importable};
use crate::settings::types::PythonVersion;

#[derive(
Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema, Hash,
Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema, Hash, EnumIter,
)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum ImportType {
Expand Down
44 changes: 41 additions & 3 deletions crates/ruff/src/rules/isort/mod.rs
Expand Up @@ -12,6 +12,7 @@ use normalize::normalize_imports;
use order::order_imports;
use settings::RelativeImportsOrder;
use sorting::cmp_either_import;
use strum::IntoEnumIterator;
use track::{Block, Trailer};
use types::EitherImport::{Import, ImportFrom};
use types::{AliasData, CommentSet, EitherImport, OrderedImportBlock, TrailingComma};
Expand Down Expand Up @@ -232,7 +233,7 @@ fn format_import_block(
target_version: PythonVersion,
) -> String {
// Categorize by type (e.g., first-party vs. third-party).
let block_by_type = categorize_imports(
let mut block_by_type = categorize_imports(
block,
src,
package,
Expand All @@ -247,7 +248,17 @@ fn format_import_block(

// Generate replacement source code.
let mut is_first_block = true;
for (import_type, import_block) in block_by_type {
let mut pending_lines_before = false;
for import_type in ImportType::iter() {
let import_block = block_by_type.remove(&import_type);

if !no_lines_before.contains(&import_type) {
pending_lines_before = true;
}
let Some(import_block) = import_block else {
continue;
};

let mut imports = order_imports(
import_block,
order_by_type,
Expand Down Expand Up @@ -280,8 +291,10 @@ fn format_import_block(
// Add a blank line between every section.
if is_first_block {
is_first_block = false;
} else if !no_lines_before.contains(&import_type) {
pending_lines_before = false;
} else if pending_lines_before {
output.push_str(stylist.line_ending());
pending_lines_before = false;
}

let mut lines_inserted = false;
Expand Down Expand Up @@ -791,6 +804,31 @@ mod tests {
Ok(())
}

#[test_case(Path::new("no_lines_before_with_empty_sections.py"))]
fn no_lines_before_with_empty_sections(path: &Path) -> Result<()> {
let snapshot = format!(
"no_lines_before_with_empty_sections.py_{}",
path.to_string_lossy()
);
let mut diagnostics = test_path(
Path::new("isort").join(path).as_path(),
&Settings {
isort: super::settings::Settings {
no_lines_before: BTreeSet::from([
ImportType::StandardLibrary,
ImportType::LocalFolder,
]),
..super::settings::Settings::default()
},
src: vec![test_resource_path("fixtures/isort")],
..Settings::for_rule(Rule::UnsortedImports)
},
)?;
diagnostics.sort_by_key(|diagnostic| diagnostic.location);
assert_yaml_snapshot!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("lines_after_imports_nothing_after.py"))]
#[test_case(Path::new("lines_after_imports_func_after.py"))]
#[test_case(Path::new("lines_after_imports_class_after.py"))]
Expand Down
@@ -0,0 +1,22 @@
---
source: crates/ruff/src/rules/isort/mod.rs
expression: diagnostics
---
- kind:
UnsortedImports: ~
location:
row: 1
column: 0
end_location:
row: 4
column: 0
fix:
content: "from __future__ import annotations\nfrom typing import Any\n\nfrom . import my_local_folder_object\n"
location:
row: 1
column: 0
end_location:
row: 4
column: 0
parent: ~

0 comments on commit ba61bb6

Please sign in to comment.