Consider Flask app logger as logger candidate (#4253)

This commit is contained in:
Dhruv Manilawala 2023-05-06 21:01:10 +05:30 committed by GitHub
parent 11e1380df4
commit 2c91412321
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 1 deletions

View file

@ -7,3 +7,12 @@ foo.info("Hello {}".format("World!"))
logging.log(logging.INFO, msg="Hello {}".format("World!")) logging.log(logging.INFO, msg="Hello {}".format("World!"))
logging.log(level=logging.INFO, msg="Hello {}".format("World!")) logging.log(level=logging.INFO, msg="Hello {}".format("World!"))
logging.log(msg="Hello {}".format("World!"), level=logging.INFO) logging.log(msg="Hello {}".format("World!"), level=logging.INFO)
# Flask support
import flask
from flask import current_app
from flask import current_app as app
flask.current_app.logger.info("Hello {}".format("World!"))
current_app.logger.info("Hello {}".format("World!"))
app.logger.log(logging.INFO, "Hello {}".format("World!"))

View file

@ -55,6 +55,34 @@ G001.py:9:17: G001 Logging statement uses `string.format()`
10 | logging.log(level=logging.INFO, msg="Hello {}".format("World!")) 10 | logging.log(level=logging.INFO, msg="Hello {}".format("World!"))
11 | logging.log(msg="Hello {}".format("World!"), level=logging.INFO) 11 | logging.log(msg="Hello {}".format("World!"), level=logging.INFO)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ G001 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ G001
12 |
13 | # Flask support
|
G001.py:16:31: G001 Logging statement uses `string.format()`
|
16 | from flask import current_app as app
17 |
18 | flask.current_app.logger.info("Hello {}".format("World!"))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ G001
19 | current_app.logger.info("Hello {}".format("World!"))
20 | app.logger.log(logging.INFO, "Hello {}".format("World!"))
|
G001.py:17:25: G001 Logging statement uses `string.format()`
|
17 | flask.current_app.logger.info("Hello {}".format("World!"))
18 | current_app.logger.info("Hello {}".format("World!"))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ G001
19 | app.logger.log(logging.INFO, "Hello {}".format("World!"))
|
G001.py:18:30: G001 Logging statement uses `string.format()`
|
18 | flask.current_app.logger.info("Hello {}".format("World!"))
19 | current_app.logger.info("Hello {}".format("World!"))
20 | app.logger.log(logging.INFO, "Hello {}".format("World!"))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ G001
| |

View file

@ -19,7 +19,7 @@ use crate::context::Context;
pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool { pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool {
if let ExprKind::Attribute { value, .. } = &func.node { if let ExprKind::Attribute { value, .. } = &func.node {
let Some(call_path) = (if let Some(call_path) = context.resolve_call_path(value) { let Some(call_path) = (if let Some(call_path) = context.resolve_call_path(value) {
if call_path.first().map_or(false, |module| *module == "logging") { if call_path.first().map_or(false, |module| *module == "logging") || call_path.as_slice() == ["flask", "current_app", "logger"] {
Some(call_path) Some(call_path)
} else { } else {
None None