mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
Ignore error calls with exc_info
in TRY400 (#4797)
This commit is contained in:
parent
b92be59ffe
commit
211d8e170d
7 changed files with 129 additions and 113 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue