Skip to content

Commit

Permalink
Add ExcInfoFalseInException Violation
Browse files Browse the repository at this point in the history
  • Loading branch information
qdegraaf committed Sep 8, 2023
1 parent 40f12de commit 4d932c7
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging
from logging import exception


logging.exception("foo") # OK


logging.exception("foo", exc_info=False) # LOG007


logging.exception("foo", exc_info=[]) # LOG007


exception("foo", exc_info=False) # LOG007


logging.exception("foo", exc_info=True) # OK
3 changes: 3 additions & 0 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::QuadraticListSummation) {
ruff::rules::quadratic_list_summation(checker, call);
}
if checker.enabled(Rule::ExcInfoFalseInException) {
flake8_logging::rules::exc_info_false_in_exception(checker, call);
}
}
Expr::Dict(ast::ExprDict {
keys,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Refurb, "132") => (RuleGroup::Nursery, rules::refurb::rules::CheckAndRemoveFromSet),

// flake8-logging
(Flake8Logging, "007") => (RuleGroup::Nursery, rules::flake8_logging::rules::ExcInfoFalseInException),
(Flake8Logging, "009") => (RuleGroup::Nursery, rules::flake8_logging::rules::UndocumentedWarn),

_ => return None,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod tests {
use crate::settings::Settings;
use crate::test::test_path;

#[test_case(Rule::ExcInfoFalseInException, Path::new("LOG007.py"))]
#[test_case(Rule::UndocumentedWarn, Path::new("LOG009.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use ruff_python_ast::ExprCall;

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::Truthiness;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;

/// ## What it does
/// Checks for uses of `logging.exception()` with `exc_info` set to `False`
///
/// ## Why is this bad?
/// The `exception()` method captures the exception automatically. Disabling this by setting
/// `exc_info=False` is the same as using `error()`, which is clearer and doesn’t need the
/// `exc_info`argument. This rule detects `exception()` calls with an exc_info argument that is
/// falsy.
///
/// ## Example
/// ```python
/// logging.exception("foo", exc_info=False)
/// ```
///
/// Use instead:
/// ```python
/// logging.error("foo")
/// ```
#[violation]
pub struct ExcInfoFalseInException;

impl Violation for ExcInfoFalseInException {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of `logging.exception` with falsy `exc_info`")
}
}

/// 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"]))
{
if let Some(_) = call.arguments.find_keyword("exc_info").filter(|keyword| {
Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id))
.is_falsey()
}) {
checker
.diagnostics
.push(Diagnostic::new(ExcInfoFalseInException, call.range()));
}
}
}
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/flake8_logging/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) use exc_info_false_in_exception::*;
pub(crate) use undocumented_warn::*;

mod exc_info_false_in_exception;
mod undocumented_warn;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/ruff/src/rules/flake8_logging/mod.rs
---
LOG007.py:8:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
8 | logging.exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
|

LOG007.py:11:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
11 | logging.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
|

LOG007.py:14:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
14 | exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
|


1 change: 1 addition & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d932c7

Please sign in to comment.