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

Ensure that LOG007 only triggers on .exception() calls #7524

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
logger.exception("foo") # OK
logger.exception("foo", exc_info=False) # LOG007
logger.exception("foo", exc_info=[]) # LOG007
logger.error("foo", exc_info=False) # OK
logger.info("foo", exc_info=False) # OK


from logging import exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use ruff_python_ast::ExprCall;
use ruff_python_ast::{self as ast, Expr, ExprCall};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::Truthiness;
use ruff_python_semantic::analyze::logging::is_logger_candidate;
use ruff_python_semantic::analyze::logging;
use ruff_python_stdlib::logging::LoggingLevel;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -42,10 +43,21 @@ impl Violation for ExceptionWithoutExcInfo {

/// LOG007
pub(crate) fn exception_without_exc_info(checker: &mut Checker, call: &ExprCall) {
if !is_logger_candidate(
call.func.as_ref(),
let Expr::Attribute(ast::ExprAttribute { value: _, attr, .. }) = call.func.as_ref() else {
return;
};

if !matches!(
LoggingLevel::from_attribute(attr.as_str()),
Some(LoggingLevel::Exception)
) {
return;
}

if !logging::is_logger_candidate(
&call.func,
checker.semantic(),
&["exception".to_string()],
&checker.settings.logger_objects,
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LOG007.py:9:1: LOG007 Use of `logging.exception` with falsy `exc_info`
9 | logger.exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
10 | logger.exception("foo", exc_info=[]) # LOG007
11 | logger.error("foo", exc_info=False) # OK
|

LOG007.py:10:1: LOG007 Use of `logging.exception` with falsy `exc_info`
Expand All @@ -35,6 +36,8 @@ LOG007.py:10:1: LOG007 Use of `logging.exception` with falsy `exc_info`
9 | logger.exception("foo", exc_info=False) # LOG007
10 | logger.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
11 | logger.error("foo", exc_info=False) # OK
12 | logger.info("foo", exc_info=False) # OK
|