Move logging resolver into logging.rs (#3843)

This commit is contained in:
Charlie Marsh 2023-03-31 23:50:44 -04:00 committed by GitHub
parent 88308ef9cc
commit 2f90157ce2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 43 deletions

View file

@ -1237,32 +1237,6 @@ impl<'a> SimpleCallArgs<'a> {
}
}
/// Return `true` if the given `Expr` is a potential logging call. Matches
/// `logging.error`, `logger.error`, `self.logger.error`, etc., but not
/// arbitrary `foo.error` calls.
///
/// It even matches direct `logging.error` calls even if the `logging` module
/// is aliased. Example:
/// ```python
/// import logging as bar
///
/// # This is detected to be a logger candidate
/// bar.error()
/// ```
pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool {
if let ExprKind::Attribute { value, .. } = &func.node {
let call_path = context
.resolve_call_path(value)
.unwrap_or_else(|| collect_call_path(value));
if let Some(tail) = call_path.last() {
if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") {
return true;
}
}
}
false
}
/// Check if a node is parent of a conditional branch.
pub fn on_conditional_branch<'a>(parents: &mut impl Iterator<Item = &'a Stmt>) -> bool {
parents.any(|parent| {

View file

@ -1,3 +1,7 @@
use crate::context::Context;
use crate::helpers::collect_call_path;
use rustpython_parser::ast::{Expr, ExprKind};
#[derive(Copy, Clone)]
pub enum LoggingLevel {
Debug,
@ -23,3 +27,29 @@ impl LoggingLevel {
}
}
}
/// Return `true` if the given `Expr` is a potential logging call. Matches
/// `logging.error`, `logger.error`, `self.logger.error`, etc., but not
/// arbitrary `foo.error` calls.
///
/// It even matches direct `logging.error` calls even if the `logging` module
/// is aliased. Example:
/// ```python
/// import logging as bar
///
/// # This is detected to be a logger candidate
/// bar.error()
/// ```
pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool {
if let ExprKind::Attribute { value, .. } = &func.node {
let call_path = context
.resolve_call_path(value)
.unwrap_or_else(|| collect_call_path(value));
if let Some(tail) = call_path.last() {
if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") {
return true;
}
}
}
false
}