mirror of
https://github.com/astral-sh/ruff
synced 2026-01-11 08:34:29 -05:00
Allow matplotlib.use calls to intersperse imports (#9094)
This PR allows `matplotlib.use` calls to intersperse imports without triggering `E402`. This is a pragmatic choice as it's common to require `matplotlib.use` calls prior to importing from within `matplotlib` itself. Closes https://github.com/astral-sh/ruff/issues/9091.
This commit is contained in:
@@ -35,3 +35,21 @@ pub fn is_sys_path_modification(stmt: &Stmt, semantic: &SemanticModel) -> bool {
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns `true` if a [`Stmt`] is a `matplotlib.use` activation, as in:
|
||||
/// ```python
|
||||
/// import matplotlib
|
||||
///
|
||||
/// matplotlib.use("Agg")
|
||||
/// ```
|
||||
pub fn is_matplotlib_activation(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(), ["matplotlib", "use"]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user