Skip to content

Commit

Permalink
Remove B904's lowercase exemption (#5751)
Browse files Browse the repository at this point in the history
## Summary

It looks like bugbear, [from the
start](PyCQA/flake8-bugbear#181 (comment)),
has had an exemption here to exempt `raise lower_case_var`. I looked at
Hypothesis and Trio, which are mentioned in that issue, and Hypothesis
has exactly one case of this, and Trio has none, so IMO it doesn't seem
worth special-casing.

Closes #5664.
  • Loading branch information
charliermarsh authored and konstin committed Jul 19, 2023
1 parent a1aedfe commit 16161a2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
7 changes: 4 additions & 3 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
except Exception as err:
assert err
raise Exception("No cause here...")
except BaseException as base_err:
# Might use this instead of bare raise with the `.with_traceback()` method
raise base_err
except BaseException as err:
raise err
except BaseException as err:
raise some_other_err
finally:
raise Exception("Nothing to chain from, so no warning here")

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3897,7 +3897,7 @@ where
}
}
if self.enabled(Rule::RaiseWithoutFromInsideExcept) {
flake8_bugbear::rules::raise_without_from_inside_except(self, body);
flake8_bugbear::rules::raise_without_from_inside_except(self, name, body);
}
if self.enabled(Rule::BlindExcept) {
flake8_blind_except::rules::blind_except(self, type_.as_deref(), name, body);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rustpython_parser::ast::{self, Expr, Stmt};
use rustpython_parser::ast;
use rustpython_parser::ast::Stmt;

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::RaiseStatementVisitor;
use ruff_python_ast::statement_visitor::StatementVisitor;
use ruff_python_stdlib::str::is_cased_lowercase;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -60,7 +60,11 @@ impl Violation for RaiseWithoutFromInsideExcept {
}

/// B904
pub(crate) fn raise_without_from_inside_except(checker: &mut Checker, body: &[Stmt]) {
pub(crate) fn raise_without_from_inside_except(
checker: &mut Checker,
name: Option<&str>,
body: &[Stmt],
) {
let raises = {
let mut visitor = RaiseStatementVisitor::default();
visitor.visit_body(body);
Expand All @@ -70,14 +74,28 @@ pub(crate) fn raise_without_from_inside_except(checker: &mut Checker, body: &[St
for (range, exc, cause) in raises {
if cause.is_none() {
if let Some(exc) = exc {
match exc {
Expr::Name(ast::ExprName { id, .. }) if is_cased_lowercase(id) => {}
_ => {
checker
.diagnostics
.push(Diagnostic::new(RaiseWithoutFromInsideExcept, range));
// If the raised object is bound to the same name, it's a re-raise, which is
// allowed (but may be flagged by other diagnostics).
//
// For example:
// ```python
// try:
// ...
// except ValueError as exc:
// raise exc
// ```
if let Some(name) = name {
if exc
.as_name_expr()
.map_or(false, |ast::ExprName { id, .. }| name == id)
{
continue;
}
}

checker
.diagnostics
.push(Diagnostic::new(RaiseWithoutFromInsideExcept, range));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,43 @@ B904.py:16:5: B904 Within an `except` clause, raise exceptions with `raise ... f
15 | assert err
16 | raise Exception("No cause here...")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
17 | except BaseException as base_err:
18 | # Might use this instead of bare raise with the `.with_traceback()` method
17 | except BaseException as err:
18 | raise err
|

B904.py:62:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
B904.py:20:5: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
|
60 | except Exception as e:
61 | if ...:
62 | raise RuntimeError("boom!")
18 | raise err
19 | except BaseException as err:
20 | raise some_other_err
| ^^^^^^^^^^^^^^^^^^^^ B904
21 | finally:
22 | raise Exception("Nothing to chain from, so no warning here")
|

B904.py:63:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
|
61 | except Exception as e:
62 | if ...:
63 | raise RuntimeError("boom!")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
63 | else:
64 | raise RuntimeError("bang!")
64 | else:
65 | raise RuntimeError("bang!")
|

B904.py:64:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
B904.py:65:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
|
62 | raise RuntimeError("boom!")
63 | else:
64 | raise RuntimeError("bang!")
63 | raise RuntimeError("boom!")
64 | else:
65 | raise RuntimeError("bang!")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|

B904.py:72:13: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
B904.py:73:13: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
|
70 | match 0:
71 | case 0:
72 | raise RuntimeError("boom!")
71 | match 0:
72 | case 0:
73 | raise RuntimeError("boom!")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|

Expand Down

0 comments on commit 16161a2

Please sign in to comment.