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

Avoid flagging blind exceptions with valid logging #2232

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions resources/test/fixtures/flake8_blind_except/BLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,34 @@
raise bad
except BaseException:
pass

import logging

try:
pass
except Exception:
logging.error("...")


try:
pass
except Exception:
logging.error("...", exc_info=False)


try:
pass
except Exception:
logging.error("...", exc_info=None)


try:
pass
except Exception:
logging.exception("...")


try:
pass
except Exception:
logging.error("...", exc_info=True)
11 changes: 11 additions & 0 deletions src/ast/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,17 @@ pub fn is_const_none(expr: &Expr) -> bool {
)
}

/// Return `true` if an [`Expr`] is `True`.
pub fn is_const_true(expr: &Expr) -> bool {
matches!(
&expr.node,
ExprKind::Constant {
value: Constant::Bool(true),
kind: None
},
)
}

/// Return `true` if a keyword argument is present with a non-`None` value.
pub fn has_non_none_keyword(keywords: &[Keyword], keyword: &str) -> bool {
find_keyword(keywords, keyword).map_or(false, |keyword| {
Expand Down
39 changes: 34 additions & 5 deletions src/rules/flake8_blind_except/rules.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rustpython_ast::{Expr, ExprKind, Stmt, StmtKind};

use crate::ast::helpers;
use crate::ast::helpers::{find_keyword, is_const_true};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::Diagnostic;
Expand All @@ -21,7 +23,7 @@ pub fn blind_except(
for exception in ["BaseException", "Exception"] {
if id == exception && checker.is_builtin(exception) {
// If the exception is re-raised, don't flag an error.
if !body.iter().any(|stmt| {
if body.iter().any(|stmt| {
if let StmtKind::Raise { exc, .. } = &stmt.node {
if let Some(exc) = exc {
if let ExprKind::Name { id, .. } = &exc.node {
Expand All @@ -36,11 +38,38 @@ pub fn blind_except(
false
}
}) {
checker.diagnostics.push(Diagnostic::new(
violations::BlindExcept(id.to_string()),
Range::from_located(type_),
));
continue;
}

// If the exception is logged, don't flag an error.
if body.iter().any(|stmt| {
if let StmtKind::Expr { value } = &stmt.node {
if let ExprKind::Call { func, keywords, .. } = &value.node {
if helpers::is_logger_candidate(func) {
if let ExprKind::Attribute { attr, .. } = &func.node {
if attr == "exception" {
return true;
}
if attr == "error" {
if let Some(keyword) = find_keyword(keywords, "exc_info") {
if is_const_true(&keyword.node.value) {
return true;
}
}
}
}
}
}
}
false
}) {
continue;
}

checker.diagnostics.push(Diagnostic::new(
violations::BlindExcept(id.to_string()),
Range::from_located(type_),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,34 @@ expression: diagnostics
column: 20
fix: ~
parent: ~
- kind:
BlindExcept: Exception
location:
row: 69
column: 7
end_location:
row: 69
column: 16
fix: ~
parent: ~
- kind:
BlindExcept: Exception
location:
row: 75
column: 7
end_location:
row: 75
column: 16
fix: ~
parent: ~
- kind:
BlindExcept: Exception
location:
row: 81
column: 7
end_location:
row: 81
column: 16
fix: ~
parent: ~