mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
[pycodestyle] Exempt sys.path += ...
calls (E402) (#15980)
## Summary The PR addresses issue #15886 .
This commit is contained in:
parent
d0555f7b5c
commit
24bab7e82e
4 changed files with 40 additions and 25 deletions
|
@ -8,33 +8,37 @@ use crate::SemanticModel;
|
|||
/// import sys
|
||||
///
|
||||
/// sys.path.append("../")
|
||||
/// sys.path += ["../"]
|
||||
/// ```
|
||||
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_qualified_name(func.as_ref())
|
||||
.is_some_and(|qualified_name| {
|
||||
matches!(
|
||||
qualified_name.segments(),
|
||||
[
|
||||
"sys",
|
||||
"path",
|
||||
"append"
|
||||
| "insert"
|
||||
| "extend"
|
||||
| "remove"
|
||||
| "pop"
|
||||
| "clear"
|
||||
| "reverse"
|
||||
| "sort"
|
||||
]
|
||||
)
|
||||
})
|
||||
match stmt {
|
||||
Stmt::Expr(ast::StmtExpr { value, range: _ }) => match value.as_ref() {
|
||||
Expr::Call(ast::ExprCall { func, .. }) => semantic
|
||||
.resolve_qualified_name(func.as_ref())
|
||||
.is_some_and(|qualified_name| {
|
||||
matches!(
|
||||
qualified_name.segments(),
|
||||
[
|
||||
"sys",
|
||||
"path",
|
||||
"append"
|
||||
| "insert"
|
||||
| "extend"
|
||||
| "remove"
|
||||
| "pop"
|
||||
| "clear"
|
||||
| "reverse"
|
||||
| "sort"
|
||||
]
|
||||
)
|
||||
}),
|
||||
_ => false,
|
||||
},
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => semantic
|
||||
.resolve_qualified_name(map_subscript(target))
|
||||
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["sys", "path"])),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if a [`Stmt`] is an `os.environ` modification, as in:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue