From 49763a7f7c519612da74e6a7cc4ca3ec1a917cab Mon Sep 17 00:00:00 2001 From: Hmvp Date: Fri, 20 Jun 2025 20:43:08 +0200 Subject: [PATCH] [`flake8-logging`] Avoid false positive for `exc_info=True` outside `logger.exception` (`LOG014`) (#18737) ## Summary Fixes https://github.com/astral-sh/ruff/issues/18726 by also checking if its a literal and not only that it is truthy. See also the first comment in the issue. It would have been nice to check for inheritance of BaseException but I figured that is not possible yet... ## Test Plan I added a few tests for valid input to exc_info --- .../resources/test/fixtures/flake8_logging/LOG014_0.py | 6 ++++++ .../flake8_logging/rules/exc_info_outside_except_handler.rs | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_logging/LOG014_0.py b/crates/ruff_linter/resources/test/fixtures/flake8_logging/LOG014_0.py index b3dc5f79bb..5eb188a987 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_logging/LOG014_0.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_logging/LOG014_0.py @@ -32,6 +32,12 @@ except ...: ### No errors +logging.info("", exc_info=ValueError()) +logger.info("", exc_info=ValueError()) + +logging.info("", exc_info=(exc_type, exc_value, exc_traceback)) +logger.info("", exc_info=(exc_type, exc_value, exc_traceback)) + logging.info("", exc_info=a) logger.info("", exc_info=a) diff --git a/crates/ruff_linter/src/rules/flake8_logging/rules/exc_info_outside_except_handler.rs b/crates/ruff_linter/src/rules/flake8_logging/rules/exc_info_outside_except_handler.rs index 9b62775fa0..1daf37dbb2 100644 --- a/crates/ruff_linter/src/rules/flake8_logging/rules/exc_info_outside_except_handler.rs +++ b/crates/ruff_linter/src/rules/flake8_logging/rules/exc_info_outside_except_handler.rs @@ -98,6 +98,10 @@ pub(crate) fn exc_info_outside_except_handler(checker: &Checker, call: &ExprCall return; }; + if !exc_info.value.is_literal_expr() { + return; + } + let truthiness = Truthiness::from_expr(&exc_info.value, |id| semantic.has_builtin_binding(id)); if truthiness.into_bool() != Some(true) {