Skip to content

Commit 49763a7

Browse files
authored
[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 #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
1 parent 2d25aae commit 49763a7

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

crates/ruff_linter/resources/test/fixtures/flake8_logging/LOG014_0.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def _():
3232

3333
### No errors
3434

35+
logging.info("", exc_info=ValueError())
36+
logger.info("", exc_info=ValueError())
37+
38+
logging.info("", exc_info=(exc_type, exc_value, exc_traceback))
39+
logger.info("", exc_info=(exc_type, exc_value, exc_traceback))
40+
3541
logging.info("", exc_info=a)
3642
logger.info("", exc_info=a)
3743

crates/ruff_linter/src/rules/flake8_logging/rules/exc_info_outside_except_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ pub(crate) fn exc_info_outside_except_handler(checker: &Checker, call: &ExprCall
9898
return;
9999
};
100100

101+
if !exc_info.value.is_literal_expr() {
102+
return;
103+
}
104+
101105
let truthiness = Truthiness::from_expr(&exc_info.value, |id| semantic.has_builtin_binding(id));
102106

103107
if truthiness.into_bool() != Some(true) {

0 commit comments

Comments
 (0)