-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ExcInfoFalseInException
Violation
- Loading branch information
Showing
8 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import logging | ||
from logging import exception | ||
|
||
|
||
logging.exception("foo") # OK | ||
|
||
|
||
logging.exception("foo", exc_info=False) # LOG007 | ||
|
||
|
||
logging.exception("foo", exc_info=[]) # LOG007 | ||
|
||
|
||
exception("foo", exc_info=False) # LOG007 | ||
|
||
|
||
logging.exception("foo", exc_info=True) # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
crates/ruff/src/rules/flake8_logging/rules/exc_info_false_in_exception.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use ruff_python_ast::ExprCall; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::Truthiness; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `logging.exception()` with `exc_info` set to `False` | ||
/// | ||
/// ## Why is this bad? | ||
/// The `exception()` method captures the exception automatically. Disabling this by setting | ||
/// `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) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// logging.error("foo") | ||
/// ``` | ||
#[violation] | ||
pub struct ExcInfoFalseInException; | ||
|
||
impl Violation for ExcInfoFalseInException { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use of `logging.exception` with falsy `exc_info`") | ||
} | ||
} | ||
|
||
/// LOG007 | ||
pub(crate) fn exc_info_false_in_exception(checker: &mut Checker, call: &ExprCall) { | ||
if checker | ||
.semantic() | ||
.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() | ||
}) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ExcInfoFalseInException, call.range())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) use exc_info_false_in_exception::*; | ||
pub(crate) use undocumented_warn::*; | ||
|
||
mod exc_info_false_in_exception; | ||
mod undocumented_warn; |
22 changes: 22 additions & 0 deletions
22
.../rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG007_LOG007.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_logging/mod.rs | ||
--- | ||
LOG007.py:8:1: LOG007 Use of `logging.exception` with falsy `exc_info` | ||
| | ||
8 | logging.exception("foo", exc_info=False) # LOG007 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 | ||
| | ||
|
||
LOG007.py:11:1: LOG007 Use of `logging.exception` with falsy `exc_info` | ||
| | ||
11 | logging.exception("foo", exc_info=[]) # LOG007 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 | ||
| | ||
|
||
LOG007.py:14:1: LOG007 Use of `logging.exception` with falsy `exc_info` | ||
| | ||
14 | exception("foo", exc_info=False) # LOG007 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.