Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-logging] Implement LOG007: ExceptionWithoutExcInfo #7410

Merged
merged 9 commits into from
Sep 18, 2023
Merged
Prev Previous commit
Next Next commit
Make test fixture more compact, clippy, regen snapshots
  • Loading branch information
qdegraaf committed Sep 18, 2023
commit d42e6d665e9b958516288c4aeace6baaa7cf3a85
11 changes: 2 additions & 9 deletions crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import logging
qdegraaf marked this conversation as resolved.
Show resolved Hide resolved
from logging import exception


logging.exception("foo") # OK


logging.exception("foo", exc_info=False) # LOG007


logging.exception("foo", exc_info=[]) # LOG007

from logging import exception

exception("foo", exc_info=False) # LOG007


logging.exception("foo", exc_info=True) # OK
exception("foo", exc_info=True) # OK
5 changes: 1 addition & 4 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,12 +923,9 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
// flake8-logging
(Flake8Logging, "001") => (RuleGroup::Preview, rules::flake8_logging::rules::DirectLoggerInstantiation),
(Flake8Logging, "002") => (RuleGroup::Preview, rules::flake8_logging::rules::InvalidGetLoggerArgument),
(Flake8Logging, "007") => (RuleGroup::Preview, rules::flake8_logging::rules::ExcInfoFalseInException),
(Flake8Logging, "009") => (RuleGroup::Preview, rules::flake8_logging::rules::UndocumentedWarn),

// flake8-logging
(Flake8Logging, "007") => (RuleGroup::Nursery, rules::flake8_logging::rules::ExcInfoFalseInException),
(Flake8Logging, "009") => (RuleGroup::Nursery, rules::flake8_logging::rules::UndocumentedWarn),

_ => return None,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::checkers::ast::Checker;
/// `exc_info=False` is the same as using `error()`, which is clearer and doesn’t need the
/// `exc_info`argument. This rule detects `exception()` calls with an exc_info argument that is
/// falsy.
///
///
/// ## Example
/// ```python
/// logging.exception("foo", exc_info=False)
Expand All @@ -42,10 +42,15 @@ pub(crate) fn exc_info_false_in_exception(checker: &mut Checker, call: &ExprCall
.resolve_call_path(call.func.as_ref())
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "exception"]))
{
if let Some(_) = call.arguments.find_keyword("exc_info").filter(|keyword| {
Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id))
.is_falsey()
}) {
if call
.arguments
.find_keyword("exc_info")
.filter(|keyword| {
Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id))
.is_falsey()
})
.is_some()
{
checker
.diagnostics
.push(Diagnostic::new(ExcInfoFalseInException, call.range()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
---
source: crates/ruff/src/rules/flake8_logging/mod.rs
---
LOG007.py:8:1: LOG007 Use of `logging.exception` with falsy `exc_info`
LOG007.py:4:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
8 | logging.exception("foo", exc_info=False) # LOG007
3 | logging.exception("foo") # OK
4 | logging.exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
5 | logging.exception("foo", exc_info=[]) # LOG007
|

LOG007.py:11:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
11 | logging.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
|
LOG007.py:5:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
3 | logging.exception("foo") # OK
4 | logging.exception("foo", exc_info=False) # LOG007
5 | logging.exception("foo", exc_info=[]) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
6 |
7 | from logging import exception
|

LOG007.py:14:1: LOG007 Use of `logging.exception` with falsy `exc_info`
LOG007.py:9:1: LOG007 Use of `logging.exception` with falsy `exc_info`
|
14 | exception("foo", exc_info=False) # LOG007
7 | from logging import exception
8 |
9 | exception("foo", exc_info=False) # LOG007
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007
10 | exception("foo", exc_info=True) # OK
|


Loading