From cee38f39dfb7eaca22eda3c19a4d81adc4097105 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:56:11 +0200 Subject: [PATCH] [`flake8-blind-expect`] Allow raise from in `BLE001` (#11131) ## Summary This allows `raise from` in BLE001. ```python try: ... except Exception as e: raise ValueError from e ``` Fixes #10806 ## Test Plan Test case added. --- .../test/fixtures/flake8_blind_except/BLE.py | 5 +++++ .../flake8_blind_except/rules/blind_except.rs | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_blind_except/BLE.py b/crates/ruff_linter/resources/test/fixtures/flake8_blind_except/BLE.py index b2272b8ab591e..10cca835ccaff 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_blind_except/BLE.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_blind_except/BLE.py @@ -124,3 +124,8 @@ pass except Exception: error("...", exc_info=True) + +try: + ... +except Exception as e: + raise ValueError from e diff --git a/crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs b/crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs index 77c0899fc0abb..be2262c130f30 100644 --- a/crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs +++ b/crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs @@ -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