[`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
This commit is contained in:
Brent Westbrook 2025-06-09 21:45:27 -04:00
parent d4d29858b4
commit 4ae45f3687
2 changed files with 6 additions and 3 deletions

View File

@ -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),

View File

@ -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;