Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specification of logging.Logger re-exports via logger-objects #5750

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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