From 4ae45f3687f08640c711348a393309acaa2c0f43 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Mon, 9 Jun 2025 21:45:27 -0400 Subject: [PATCH] [`flake8-logging`] Stabilize `log-exception-outside-except-handler` (`LOG004`) Summary -- Stabilizes LOG004 and updates the documentation to say explicitly that the rule still triggers even when passing the `exc_info` kwarg (https://github.com/astral-sh/ruff/issues/18044). Test Plan -- Existing tests, which were already in the right place --- crates/ruff_linter/src/codes.rs | 2 +- .../rules/log_exception_outside_except_handler.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 3e15e1fd36..d900be2309 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -1148,7 +1148,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { // flake8-logging (Flake8Logging, "001") => (RuleGroup::Stable, rules::flake8_logging::rules::DirectLoggerInstantiation), (Flake8Logging, "002") => (RuleGroup::Stable, rules::flake8_logging::rules::InvalidGetLoggerArgument), - (Flake8Logging, "004") => (RuleGroup::Preview, rules::flake8_logging::rules::LogExceptionOutsideExceptHandler), + (Flake8Logging, "004") => (RuleGroup::Stable, rules::flake8_logging::rules::LogExceptionOutsideExceptHandler), (Flake8Logging, "007") => (RuleGroup::Stable, rules::flake8_logging::rules::ExceptionWithoutExcInfo), (Flake8Logging, "009") => (RuleGroup::Stable, rules::flake8_logging::rules::UndocumentedWarn), (Flake8Logging, "014") => (RuleGroup::Preview, rules::flake8_logging::rules::ExcInfoOutsideExceptHandler), diff --git a/crates/ruff_linter/src/rules/flake8_logging/rules/log_exception_outside_except_handler.rs b/crates/ruff_linter/src/rules/flake8_logging/rules/log_exception_outside_except_handler.rs index 1f2eb173bd..5a7062cb1b 100644 --- a/crates/ruff_linter/src/rules/flake8_logging/rules/log_exception_outside_except_handler.rs +++ b/crates/ruff_linter/src/rules/flake8_logging/rules/log_exception_outside_except_handler.rs @@ -11,7 +11,7 @@ use crate::{Edit, Fix, FixAvailability, Violation}; /// Checks for `.exception()` logging calls outside of exception handlers. /// /// ## Why is this bad? -/// [The documentation] states: +/// The Python `logging` [documentation] states: /// > This function should only be called from an exception handler. /// /// Calling `.exception()` outside of an exception handler @@ -23,6 +23,9 @@ use crate::{Edit, Fix, FixAvailability, Violation}; /// NoneType: None /// ``` /// +/// Although this confusion can be avoided by passing an explicit `exc_info` keyword argument, this +/// rule will still emit a diagnostic, in line with the `logging` documentation. +/// /// ## Example /// /// ```python @@ -42,7 +45,7 @@ use crate::{Edit, Fix, FixAvailability, Violation}; /// ## Fix safety /// The fix, if available, will always be marked as unsafe, as it changes runtime behavior. /// -/// [The documentation]: https://docs.python.org/3/library/logging.html#logging.exception +/// [documentation]: https://docs.python.org/3/library/logging.html#logging.exception #[derive(ViolationMetadata)] pub(crate) struct LogExceptionOutsideExceptHandler;