Skip to content

Commit

Permalink
[flake8-blind-expect] Allow raise from in BLE001 (#11131)
Browse files Browse the repository at this point in the history
## Summary

This allows `raise from` in BLE001.

```python
try:
    ...
except Exception as e:
    raise ValueError from e
```

Fixes #10806

## Test Plan

Test case added.
  • Loading branch information
autinerd committed Apr 24, 2024
1 parent e3fde28 commit cee38f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@
pass
except Exception:
error("...", exc_info=True)

try:
...
except Exception as e:
raise ValueError from e
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,25 @@ pub(crate) fn blind_except(
if !matches!(builtin_exception_type, "BaseException" | "Exception") {
return;
}

// If the exception is re-raised, don't flag an error.
if body.iter().any(|stmt| {
if let Stmt::Raise(ast::StmtRaise { exc, .. }) = stmt {
if let Some(exc) = exc {
if let Expr::Name(ast::ExprName { id, .. }) = exc.as_ref() {
if let Stmt::Raise(ast::StmtRaise { exc, cause, .. }) = stmt {
if let Some(cause) = cause {
if let Expr::Name(ast::ExprName { id, .. }) = cause.as_ref() {
name.is_some_and(|name| id == name)
} else {
false
}
} else {
true
if let Some(exc) = exc {
if let Expr::Name(ast::ExprName { id, .. }) = exc.as_ref() {
name.is_some_and(|name| id == name)
} else {
false
}
} else {
true
}
}
} else {
false
Expand Down

0 comments on commit cee38f3

Please sign in to comment.