mirror of
https://github.com/astral-sh/ruff
synced 2026-01-08 15:14:19 -05:00
Ignore error calls with exc_info in TRY400 (#4797)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use rustpython_parser::ast::{self, Expr};
|
||||
use rustpython_parser::ast::{self, Constant, Expr, Keyword};
|
||||
|
||||
use ruff_python_ast::call_path::collect_call_path;
|
||||
use ruff_python_ast::helpers::find_keyword;
|
||||
|
||||
use crate::model::SemanticModel;
|
||||
|
||||
@@ -37,3 +38,31 @@ pub fn is_logger_candidate(func: &Expr, model: &SemanticModel) -> bool {
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// If the keywords to a logging call contain `exc_info=True` or `exc_info=sys.exc_info()`,
|
||||
/// return the `Keyword` for `exc_info`.
|
||||
pub fn exc_info<'a>(keywords: &'a [Keyword], model: &SemanticModel) -> Option<&'a Keyword> {
|
||||
let exc_info = find_keyword(keywords, "exc_info")?;
|
||||
|
||||
// Ex) `logging.error("...", exc_info=True)`
|
||||
if matches!(
|
||||
exc_info.value,
|
||||
Expr::Constant(ast::ExprConstant {
|
||||
value: Constant::Bool(true),
|
||||
..
|
||||
})
|
||||
) {
|
||||
return Some(exc_info);
|
||||
}
|
||||
|
||||
// Ex) `logging.error("...", exc_info=sys.exc_info())`
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = &exc_info.value {
|
||||
if model.resolve_call_path(func).map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["sys", "exc_info"]
|
||||
}) {
|
||||
return Some(exc_info);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user