mirror of https://github.com/astral-sh/ruff
[`I001`] fix isort for files with tab-based indentation (#2361)
This PR fixes two related issues with using isort on files using tabs for indentation: - Multiline imports are never considered correctly formatted, since the comparison with the generated code will always fail. - Using autofix generates code that can have mixed indentation in the same line, for imports that are within nested blocks.
This commit is contained in:
parent
01fedec1e7
commit
5ac5b69e9f
|
|
@ -0,0 +1,29 @@
|
|||
from numpy import (
|
||||
cos,
|
||||
int8,
|
||||
int16,
|
||||
int32,
|
||||
int64,
|
||||
sin,
|
||||
tan,
|
||||
uint8,
|
||||
uint16,
|
||||
uint32,
|
||||
uint64,
|
||||
)
|
||||
|
||||
if True:
|
||||
# inside nested block
|
||||
from numpy import (
|
||||
cos,
|
||||
int8,
|
||||
int16,
|
||||
int32,
|
||||
int64,
|
||||
sin,
|
||||
tan,
|
||||
uint8,
|
||||
uint16,
|
||||
uint32,
|
||||
uint64,
|
||||
)
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
use super::types::{AliasData, CommentSet, ImportFromData, Importable};
|
||||
use crate::source_code::Stylist;
|
||||
|
||||
// Hard-code four-space indentation for the imports themselves, to match Black.
|
||||
const INDENT: &str = " ";
|
||||
|
||||
// Guess a capacity to use for string allocation.
|
||||
const CAPACITY: usize = 200;
|
||||
|
||||
|
|
@ -174,11 +171,11 @@ fn format_multi_line(
|
|||
|
||||
for (AliasData { name, asname }, comments) in aliases {
|
||||
for comment in &comments.atop {
|
||||
output.push_str(INDENT);
|
||||
output.push_str(stylist.indentation());
|
||||
output.push_str(comment);
|
||||
output.push_str(stylist.line_ending());
|
||||
}
|
||||
output.push_str(INDENT);
|
||||
output.push_str(stylist.indentation());
|
||||
if let Some(asname) = asname {
|
||||
output.push_str(name);
|
||||
output.push_str(" as ");
|
||||
|
|
|
|||
|
|
@ -714,6 +714,7 @@ mod tests {
|
|||
#[test_case(Path::new("preserve_comment_order.py"))]
|
||||
#[test_case(Path::new("preserve_import_star.py"))]
|
||||
#[test_case(Path::new("preserve_indentation.py"))]
|
||||
#[test_case(Path::new("preserve_tabs.py"))]
|
||||
#[test_case(Path::new("relative_imports_order.py"))]
|
||||
#[test_case(Path::new("reorder_within_section.py"))]
|
||||
#[test_case(Path::new("separate_first_party_imports.py"))]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
source: src/rules/isort/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
[]
|
||||
|
||||
Loading…
Reference in New Issue