Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from __future__ import annotations
# EOF
27 changes: 24 additions & 3 deletions crates/ruff_linter/src/importer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ impl<'a> Importer<'a> {
// Insert after the last top-level import.
Insertion::end_of_statement(stmt, self.source, self.stylist).into_edit(&required_import)
} else {
// Insert at the start of the file.
Insertion::start_of_file(self.python_ast, self.source, self.stylist)
.into_edit(&required_import)
// Check if there are any future imports that we need to respect
if let Some(last_future_import) = self.find_last_future_import() {
// Insert after the last future import
Insertion::end_of_statement(last_future_import, self.source, self.stylist)
.into_edit(&required_import)
} else {
// Insert at the start of the file.
Insertion::start_of_file(self.python_ast, self.source, self.stylist)
.into_edit(&required_import)
}
}
}

Expand Down Expand Up @@ -524,6 +531,20 @@ impl<'a> Importer<'a> {
}
}

/// Find the last `from __future__` import statement in the AST.
fn find_last_future_import(&self) -> Option<&'a Stmt> {
self.python_ast
.iter()
.take_while(|stmt| {
if let Stmt::ImportFrom(import_from) = stmt {
import_from.module.as_deref() == Some("__future__")
} else {
false
}
})
.last()
Comment thread
ntBre marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could take some inspiration from the match_docstring_end helper and just skip one statement. It's not a huge deal since something like this is a separate syntax error, but it still seems a bit more precise:

"docstring"
"another string"
from __future__ import annotations  # SyntaxError: from __future__ imports must occur at the beginning of the file

What do you think about something like this?

    /// Find the last valid `from __future__` import statement in the AST.
    fn find_last_future_import(&self) -> Option<&'a Stmt> {
        let mut body = self.python_ast.iter().peekable();
        let _docstring = body.next_if(|stmt| is_docstring_stmt(stmt));

        body.take_while(|stmt| {
            stmt.as_import_from_stmt()
                .is_some_and(|import_from| import_from.module.as_deref() == Some("__future__"))
        })
        .last()
    }

}

/// Add a `from __future__ import annotations` import.
pub(crate) fn add_future_import(&self) -> Edit {
let import = &NameImport::ImportFrom(MemberNameImport::member(
Expand Down
23 changes: 23 additions & 0 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,29 @@ mod tests {
Ok(())
}

#[test_case(Path::new("future_import.py"))]
fn required_import_with_future_import(path: &Path) -> Result<()> {
let snapshot = format!(
"required_import_with_future_import_{}",
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("isort/required_imports").join(path).as_path(),
&LinterSettings {
src: vec![test_resource_path("fixtures/isort")],
isort: super::settings::Settings {
required_imports: BTreeSet::from_iter([NameImport::Import(
ModuleNameImport::module("this".to_string()),
)]),
..super::settings::Settings::default()
},
..LinterSettings::for_rule(Rule::MissingRequiredImport)
},
)?;
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("from_first.py"))]
fn from_first(path: &Path) -> Result<()> {
let snapshot = format!("from_first_{}", path.to_string_lossy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ source: crates/ruff_linter/src/rules/isort/mod.rs
I002 [*] Missing required import: `from __future__ import annotations`
--> existing_import.py:1:1
help: Insert required import: `from __future__ import annotations`
1 + from __future__ import annotations
2 | from __future__ import generator_stop
1 | from __future__ import generator_stop
2 + from __future__ import annotations
Comment thread
amyreese marked this conversation as resolved.
3 | import os
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ source: crates/ruff_linter/src/rules/isort/mod.rs
I002 [*] Missing required import: `from __future__ import annotations as _annotations`
--> existing_import.py:1:1
help: Insert required import: `from __future__ import annotations as _annotations`
1 + from __future__ import annotations as _annotations
2 | from __future__ import generator_stop
1 | from __future__ import generator_stop
2 + from __future__ import annotations as _annotations
3 | import os
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
I002 [*] Missing required import: `import this`
--> future_import.py:1:1
help: Insert required import: `import this`
1 | from __future__ import annotations
2 + import this
3 | # EOF