Skip to content

Commit

Permalink
Detect local logging module re-exports in flake8-logging-format
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 13, 2023
1 parent af2a087 commit d8a6e9c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
from distutils import log

from logging_setup import logger

logging.warn("Hello World!")
log.warn("Hello world!") # This shouldn't be considered as a logger candidate
logger.warn("Hello world!")
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
---
source: crates/ruff/src/rules/flake8_logging_format/mod.rs
---
G010.py:4:9: G010 [*] Logging statement uses `warn` instead of `warning`
G010.py:6:9: G010 [*] Logging statement uses `warn` instead of `warning`
|
2 | from distutils import log
3 |
4 | logging.warn("Hello World!")
4 | from logging_setup import logger
5 |
6 | logging.warn("Hello World!")
| ^^^^ G010
5 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
8 | logger.warn("Hello world!")
|
= help: Convert to `warn`

Fix
1 1 | import logging
2 2 | from distutils import log
3 3 |
4 |-logging.warn("Hello World!")
4 |+logging.warning("Hello World!")
5 5 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
4 4 | from logging_setup import logger
5 5 |
6 |-logging.warn("Hello World!")
6 |+logging.warning("Hello World!")
7 7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
8 8 | logger.warn("Hello world!")

G010.py:8:8: G010 [*] Logging statement uses `warn` instead of `warning`
|
6 | logging.warn("Hello World!")
7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
8 | logger.warn("Hello world!")
| ^^^^ G010
|
= help: Convert to `warn`

Fix
5 5 |
6 6 | logging.warn("Hello World!")
7 7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate
8 |-logger.warn("Hello world!")
8 |+logger.warning("Hello world!")


30 changes: 13 additions & 17 deletions crates/ruff_python_semantic/src/analyze/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ use crate::model::SemanticModel;
/// ```
pub fn is_logger_candidate(func: &Expr, semantic: &SemanticModel) -> bool {
if let Expr::Attribute(ast::ExprAttribute { value, .. }) = func {
let Some(call_path) = (if let Some(call_path) = semantic.resolve_call_path(value) {
if call_path
.first()
.map_or(false, |module| *module == "logging")
|| call_path.as_slice() == ["flask", "current_app", "logger"]
{
Some(call_path)
} else {
None
if let Some(call_path) = semantic
.resolve_call_path(value)
.or_else(|| collect_call_path(value))
{
// Avoid some false positives.
if matches!(call_path.as_slice(), ["distutils", "log"]) {
return false;
}
} else {
collect_call_path(value)
}) else {
return false;
};
if let Some(tail) = call_path.last() {
if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") {
return true;

if let Some(tail) = call_path.last() {
if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging")
{
return true;
}
}
}
}
Expand Down

0 comments on commit d8a6e9c

Please sign in to comment.