mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 16:40:19 +00:00
Allow sys.path
modifications between imports (#9047)
## Summary It's common to interleave a `sys.path` modification between imports at the top of a file. This is a frequent cause of `# noqa: E402` false positives, as seen in the ecosystem checks. This PR modifies E402 to omit such modifications when determining the "import boundary". (We could consider linting against `sys.path` modifications, but that should be a separate rule.) Closes: https://github.com/astral-sh/ruff/issues/5557.
This commit is contained in:
parent
96ae9fe685
commit
b021ede481
8 changed files with 110 additions and 22 deletions
37
crates/ruff_python_semantic/src/analyze/imports.rs
Normal file
37
crates/ruff_python_semantic/src/analyze/imports.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use ruff_python_ast::{self as ast, Expr, Stmt};
|
||||
|
||||
use crate::SemanticModel;
|
||||
|
||||
/// Returns `true` if a [`Stmt`] is a `sys.path` modification, as in:
|
||||
/// ```python
|
||||
/// import sys
|
||||
///
|
||||
/// sys.path.append("../")
|
||||
/// ```
|
||||
pub fn is_sys_path_modification(stmt: &Stmt, semantic: &SemanticModel) -> bool {
|
||||
let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt else {
|
||||
return false;
|
||||
};
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
semantic
|
||||
.resolve_call_path(func.as_ref())
|
||||
.is_some_and(|call_path| {
|
||||
matches!(
|
||||
call_path.as_slice(),
|
||||
[
|
||||
"sys",
|
||||
"path",
|
||||
"append"
|
||||
| "insert"
|
||||
| "extend"
|
||||
| "remove"
|
||||
| "pop"
|
||||
| "clear"
|
||||
| "reverse"
|
||||
| "sort"
|
||||
]
|
||||
)
|
||||
})
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
pub mod function_type;
|
||||
pub mod imports;
|
||||
pub mod logging;
|
||||
pub mod type_inference;
|
||||
pub mod typing;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue