Allow aliased logging module as a logger candidate (#3718)

This commit is contained in:
Dhruv Manilawala 2023-03-25 02:49:09 +05:30 committed by GitHub
parent 7af83460ce
commit 63adf9f5e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 11 deletions

View file

@ -1288,9 +1288,20 @@ 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.
pub fn is_logger_candidate(func: &Expr) -> bool {
///
/// 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 = collect_call_path(value);
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;