Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes #20674

@github-actions
Copy link
Contributor

github-actions bot commented Oct 2, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines 536 to 542
self.python_ast.iter().rev().find(|stmt| {
if let Stmt::ImportFrom(import_from) = stmt {
import_from.module.as_deref() == Some("__future__")
} else {
false
}
})
Copy link
Contributor

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()
}

@danparizher danparizher requested a review from amyreese October 2, 2025 01:09
@amyreese amyreese added rule Implementing or modifying a lint rule isort Related to import sorting fixes Related to suggested fixes for violations labels Oct 2, 2025
@amyreese amyreese requested a review from ntBre October 2, 2025 15:46
Copy link
Contributor

@ntBre ntBre left a 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.

@danparizher danparizher requested a review from ntBre October 2, 2025 22:29
false
}
})
.last()
Copy link
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()
    }

Ok(())
}

#[test_case(Path::new("docstring_future_import.py"))]
Copy link
Contributor

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.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre ntBre enabled auto-merge (squash) October 6, 2025 13:36
@ntBre ntBre merged commit 9a29f7a into astral-sh:main Oct 6, 2025
35 checks passed
@danparizher danparizher deleted the fix-20674 branch October 6, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations isort Related to import sorting rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I002 inserts required imports before future imports

3 participants