Ignore TRY301 exceptions without except handlers (#4301)

This commit is contained in:
Charlie Marsh 2023-05-08 23:38:02 -04:00 committed by GitHub
parent 9366eb919d
commit 83536cf87b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View file

@ -45,3 +45,10 @@ def good():
logger.exception("a failed")
except Exception:
logger.exception("something failed")
def fine():
try:
a = process() # This throws the exception now
finally:
print("finally")

View file

@ -1811,7 +1811,7 @@ where
tryceratops::rules::verbose_log_message(self, handlers);
}
if self.settings.rules.enabled(Rule::RaiseWithinTry) {
tryceratops::rules::raise_within_try(self, body);
tryceratops::rules::raise_within_try(self, body, handlers);
}
if self.settings.rules.enabled(Rule::ErrorInsteadOfException) {
tryceratops::rules::error_instead_of_exception(self, handlers);

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{Stmt, StmtKind};
use rustpython_parser::ast::{Excepthandler, Stmt, StmtKind};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
@ -72,7 +72,11 @@ where
}
/// TRY301
pub fn raise_within_try(checker: &mut Checker, body: &[Stmt]) {
pub fn raise_within_try(checker: &mut Checker, body: &[Stmt], handlers: &[Excepthandler]) {
if handlers.is_empty() {
return;
}
let raises = {
let mut visitor = RaiseStatementVisitor::default();
for stmt in body {