[flake8-logging] Avoid false positive for exc_info=True outside logger.exception (LOG014) (#18737)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## 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
This commit is contained in:
Hmvp 2025-06-20 20:43:08 +02:00 committed by GitHub
parent 2d25aaeaa2
commit 49763a7f7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View file

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

View file

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