mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:34:57 +00:00
Allow non-verbose raise when cause is present (#2816)
The motivating issue here is of the following form: ```py try: raise Exception("We want to hide this error message") except Exception: try: raise Exception("We want to show this") except Exception as exc: raise exc from None ``` However, I think we should avoid this if _any_ cause is present, since causes require a named exception. Closes #2814.
This commit is contained in:
parent
9ddd5e4cfe
commit
099d5414f2
3 changed files with 29 additions and 14 deletions
|
@ -27,6 +27,7 @@ def good():
|
|||
logger.exception("process failed")
|
||||
raise
|
||||
|
||||
|
||||
def still_good():
|
||||
try:
|
||||
process()
|
||||
|
@ -35,6 +36,14 @@ def still_good():
|
|||
raise
|
||||
|
||||
|
||||
def still_good_too():
|
||||
try:
|
||||
process()
|
||||
except MyException as e:
|
||||
print(e)
|
||||
raise e from None
|
||||
|
||||
|
||||
def still_actually_good():
|
||||
try:
|
||||
process()
|
||||
|
@ -60,5 +69,6 @@ def bad_that_needs_recursion_2():
|
|||
except MyException as e:
|
||||
logger.exception("process failed")
|
||||
if True:
|
||||
|
||||
def foo():
|
||||
raise e
|
||||
|
|
|
@ -20,7 +20,7 @@ impl Violation for VerboseRaise {
|
|||
|
||||
#[derive(Default)]
|
||||
struct RaiseStatementVisitor<'a> {
|
||||
raises: Vec<Option<&'a Expr>>,
|
||||
raises: Vec<(Option<&'a Expr>, Option<&'a Expr>)>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Visitor<'b> for RaiseStatementVisitor<'a>
|
||||
|
@ -29,7 +29,7 @@ where
|
|||
{
|
||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||
match &stmt.node {
|
||||
StmtKind::Raise { exc, .. } => self.raises.push(exc.as_ref().map(|expr| &**expr)),
|
||||
StmtKind::Raise { exc, cause } => self.raises.push((exc.as_deref(), cause.as_deref())),
|
||||
StmtKind::Try {
|
||||
body, finalbody, ..
|
||||
} => {
|
||||
|
@ -59,13 +59,18 @@ pub fn verbose_raise(checker: &mut Checker, handlers: &[Excepthandler]) {
|
|||
}
|
||||
visitor.raises
|
||||
};
|
||||
for expr in raises.into_iter().flatten() {
|
||||
// ...and the raised object is bound to the same name...
|
||||
if let ExprKind::Name { id, .. } = &expr.node {
|
||||
if id == exception_name {
|
||||
checker
|
||||
.diagnostics
|
||||
.push(Diagnostic::new(VerboseRaise, Range::from_located(expr)));
|
||||
for (exc, cause) in raises {
|
||||
if cause.is_some() {
|
||||
continue;
|
||||
}
|
||||
if let Some(exc) = exc {
|
||||
// ...and the raised object is bound to the same name...
|
||||
if let ExprKind::Name { id, .. } = &exc.node {
|
||||
if id == exception_name {
|
||||
checker
|
||||
.diagnostics
|
||||
.push(Diagnostic::new(VerboseRaise, Range::from_located(exc)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
source: src/rules/tryceratops/mod.rs
|
||||
source: crates/ruff/src/rules/tryceratops/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
|
@ -15,20 +15,20 @@ expression: diagnostics
|
|||
- kind:
|
||||
VerboseRaise: ~
|
||||
location:
|
||||
row: 54
|
||||
row: 63
|
||||
column: 18
|
||||
end_location:
|
||||
row: 54
|
||||
row: 63
|
||||
column: 19
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
VerboseRaise: ~
|
||||
location:
|
||||
row: 64
|
||||
row: 74
|
||||
column: 22
|
||||
end_location:
|
||||
row: 64
|
||||
row: 74
|
||||
column: 23
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue