Skip to content

Commit

Permalink
Allow sys.path modifications between imports
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 7, 2023
1 parent fcc0889 commit 9e14c1d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ impl<'a> Checker<'a> {
}
}

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"
]
)
})
}

impl<'a, 'b> Visitor<'b> for Checker<'a>
where
'b: 'a,
Expand Down Expand Up @@ -306,6 +334,7 @@ where
if !self.semantic.seen_import_boundary()
&& !helpers::is_assignment_to_a_dunder(stmt)
&& !helpers::in_nested_block(self.semantic.current_statements())
&& !is_sys_path_modification(stmt, self.semantic())
{
self.semantic.flags |= SemanticModelFlags::IMPORT_BOUNDARY;
}
Expand Down

0 comments on commit 9e14c1d

Please sign in to comment.