Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isort no-lines-before preceded by an empty section #3139

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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: ~