-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[isort] Fix inserting required imports before future imports (I002)
#20676
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
Conversation
|
| self.python_ast.iter().rev().find(|stmt| { | ||
| if let Stmt::ImportFrom(import_from) = stmt { | ||
| import_from.module.as_deref() == Some("__future__") | ||
| } else { | ||
| false | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since future imports can only be at the beginning, iterating over all statements is inefficient, particularly on large files with many statements. Instead, only look through statements until you've found something that's not a future import.
Eg, something like:
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()
}
...es/isort/snapshots/ruff_linter__rules__isort__tests__required_import_existing_import.py.snap
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to handle docstrings, and I also had some ideas for other places to add this check.
| false | ||
| } | ||
| }) | ||
| .last() |
There was a problem hiding this comment.
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 fileWhat 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()
}| Ok(()) | ||
| } | ||
|
|
||
| #[test_case(Path::new("docstring_future_import.py"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make these two test_cases on the same test function? The bodies of the functions look pretty much the same to me, besides the snapshot name. I think using the test case path is enough differentiation in the filename, though.
…ng_future_import_docstring_future_import.py.snap
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary
Fixes #20674