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

[flake8-logging] Implement LOG007: ExceptionWithoutExcInfo #7410

Merged
merged 9 commits into from
Sep 18, 2023
Prev Previous commit
Next Next commit
Also flag logger.exception() calls
  • Loading branch information
qdegraaf committed Sep 18, 2023
commit 662e9c1200c32c5963deefd11bb6e710cb93d5f3
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -37,11 +38,19 @@ impl Violation for ExcInfoFalseInException {

/// LOG007
pub(crate) fn exc_info_false_in_exception(checker: &mut Checker, call: &ExprCall) {
if checker
.semantic()
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "exception"]))
{
let Expr::Attribute(ast::ExprAttribute { attr, .. }) = call.func.as_ref() else {
return;
};

if attr.as_str() != "exception" {
return;
}

if is_logger_candidate(
call.func.as_ref(),
checker.semantic(),
&["exception".to_string()],
) {
if call
.arguments
.find_keyword("exc_info")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
---
source: crates/ruff/src/rules/flake8_logging/mod.rs
---
LOG007.py:4:1: LOG007 Use of `logging.exception` with falsy `exc_info`
LOG007.py:6:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
3 | logging.exception("foo") # OK
4 | logging.exception("foo", exc_info=False) # LOG007
5 | logging.exception("foo") # OK
6 | logging.exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
5 | logging.exception("foo", exc_info=[]) # LOG007
7 | logging.exception("foo", exc_info=[]) # LOG007
8 | logger.exception("foo") # OK
|

LOG007.py:5:1: LOG007 Use of `logging.exception` with falsy `exc_info`
LOG007.py:7:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
3 | logging.exception("foo") # OK
4 | logging.exception("foo", exc_info=False) # LOG007
5 | logging.exception("foo", exc_info=[]) # LOG007
5 | logging.exception("foo") # OK
6 | logging.exception("foo", exc_info=False) # LOG007
7 | logging.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
6 |
7 | from logging import exception
8 | logger.exception("foo") # OK
9 | logger.exception("foo", exc_info=False) # LOG007
|

LOG007.py:9:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
7 | from logging import exception
8 |
9 | exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
10 | exception("foo", exc_info=True) # OK
7 | logging.exception("foo", exc_info=[]) # LOG007
8 | logger.exception("foo") # OK
9 | logger.exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
10 | logger.exception("foo", exc_info=[]) # LOG007
|

LOG007.py:10:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
8 | logger.exception("foo") # OK
9 | logger.exception("foo", exc_info=False) # LOG007
10 | logger.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
|


Loading