-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #5557.
- Loading branch information
1 parent
96ae9fe
commit b021ede
Showing
8 changed files
with
110 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 22 additions & 12 deletions
34
...src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__E402_E402.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
E402.py:29:1: E402 Module level import not at top of file | ||
| | ||
27 | __some__magic = 1 | ||
28 | | ||
29 | import g | ||
| ^^^^^^^^ E402 | ||
| | ||
|
||
E402.py:39:1: E402 Module level import not at top of file | ||
| | ||
37 | import i | ||
38 | | ||
39 | import j; import k | ||
| ^^^^^^^^ E402 | ||
| | ||
|
||
E402.py:39:11: E402 Module level import not at top of file | ||
| | ||
37 | import i | ||
38 | | ||
39 | import j; import k | ||
| ^^^^^^^^ E402 | ||
| | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod function_type; | ||
pub mod imports; | ||
pub mod logging; | ||
pub mod type_inference; | ||
pub mod typing; | ||
|