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
Doc tweaks
  • Loading branch information
charliermarsh committed Sep 18, 2023
commit c99cdca0689dee355a60d75b808a6be05e77f063
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@

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

Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ 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`
/// 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.
/// The `logging.exception()` method captures the exception automatically, but
/// accepts an optional `exc_info` argument to override this behavior. Setting
/// `exc_info` to `False` disables the automatic capture of the exception and
/// stack trace.
///
/// Instead of setting `exc_info` to `False`, prefer `logging.error()`, which
/// has equivalent behavior to `logging.exception()` with `exc_info` set to
/// `False`, but is clearer in intent.
///
/// ## Example
/// ```python
/// logging.exception("foo", exc_info=False)
/// logging.exception("...", exc_info=False)
/// ```
///
/// Use instead:
/// ```python
/// logging.error("foo")
/// logging.error("...")
/// ```
#[violation]
pub struct ExcInfoFalseInException;
Expand All @@ -38,23 +42,24 @@ impl Violation for ExcInfoFalseInException {

/// LOG007
pub(crate) fn exc_info_false_in_exception(checker: &mut Checker, call: &ExprCall) {
if is_logger_candidate(
if !is_logger_candidate(
call.func.as_ref(),
checker.semantic(),
&["exception".to_string()],
) {
if call
.arguments
.find_keyword("exc_info")
.filter(|keyword| {
Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id))
.is_falsey()
})
.is_some()
{
checker
.diagnostics
.push(Diagnostic::new(ExcInfoFalseInException, call.range()));
}
return;
}

if call
.arguments
.find_keyword("exc_info")
.map(|keyword| &keyword.value)
.is_some_and(|value| {
Truthiness::from_expr(value, |id| checker.semantic().is_builtin(id)).is_falsey()
})
{
checker
.diagnostics
.push(Diagnostic::new(ExcInfoFalseInException, call.range()));
}
}
Loading