mirror of https://github.com/astral-sh/ruff
Remove `B904`'s lowercase exemption (#5751)
## Summary It looks like bugbear, [from the start](https://github.com/PyCQA/flake8-bugbear/pull/181#issuecomment-904314876), 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 https://github.com/astral-sh/ruff/issues/5664.
This commit is contained in:
parent
816f7644a9
commit
513de13c46
|
|
@ -14,9 +14,10 @@ except AssertionError:
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
assert err
|
assert err
|
||||||
raise Exception("No cause here...")
|
raise Exception("No cause here...")
|
||||||
except BaseException as base_err:
|
except BaseException as err:
|
||||||
# Might use this instead of bare raise with the `.with_traceback()` method
|
raise err
|
||||||
raise base_err
|
except BaseException as err:
|
||||||
|
raise some_other_err
|
||||||
finally:
|
finally:
|
||||||
raise Exception("Nothing to chain from, so no warning here")
|
raise Exception("Nothing to chain from, so no warning here")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3897,7 +3897,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.enabled(Rule::RaiseWithoutFromInsideExcept) {
|
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) {
|
if self.enabled(Rule::BlindExcept) {
|
||||||
flake8_blind_except::rules::blind_except(self, type_.as_deref(), name, body);
|
flake8_blind_except::rules::blind_except(self, type_.as_deref(), name, body);
|
||||||
|
|
|
||||||
|
|
@ -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_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::RaiseStatementVisitor;
|
use ruff_python_ast::helpers::RaiseStatementVisitor;
|
||||||
use ruff_python_ast::statement_visitor::StatementVisitor;
|
use ruff_python_ast::statement_visitor::StatementVisitor;
|
||||||
use ruff_python_stdlib::str::is_cased_lowercase;
|
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
|
|
@ -60,7 +60,11 @@ impl Violation for RaiseWithoutFromInsideExcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// B904
|
/// 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 raises = {
|
||||||
let mut visitor = RaiseStatementVisitor::default();
|
let mut visitor = RaiseStatementVisitor::default();
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
|
|
@ -70,15 +74,29 @@ pub(crate) fn raise_without_from_inside_except(checker: &mut Checker, body: &[St
|
||||||
for (range, exc, cause) in raises {
|
for (range, exc, cause) in raises {
|
||||||
if cause.is_none() {
|
if cause.is_none() {
|
||||||
if let Some(exc) = exc {
|
if let Some(exc) = exc {
|
||||||
match exc {
|
// If the raised object is bound to the same name, it's a re-raise, which is
|
||||||
Expr::Name(ast::ExprName { id, .. }) if is_cased_lowercase(id) => {}
|
// 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
|
checker
|
||||||
.diagnostics
|
.diagnostics
|
||||||
.push(Diagnostic::new(RaiseWithoutFromInsideExcept, range));
|
.push(Diagnostic::new(RaiseWithoutFromInsideExcept, range));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,33 +27,43 @@ B904.py:16:5: B904 Within an `except` clause, raise exceptions with `raise ... f
|
||||||
15 | assert err
|
15 | assert err
|
||||||
16 | raise Exception("No cause here...")
|
16 | raise Exception("No cause here...")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
||||||
17 | except BaseException as base_err:
|
17 | except BaseException as err:
|
||||||
18 | # Might use this instead of bare raise with the `.with_traceback()` method
|
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:
|
18 | raise err
|
||||||
61 | if ...:
|
19 | except BaseException as err:
|
||||||
62 | raise RuntimeError("boom!")
|
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
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
||||||
63 | else:
|
64 | else:
|
||||||
64 | raise RuntimeError("bang!")
|
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 | raise RuntimeError("boom!")
|
||||||
63 | else:
|
64 | else:
|
||||||
64 | raise RuntimeError("bang!")
|
65 | raise RuntimeError("bang!")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 | match 0:
|
||||||
71 | case 0:
|
72 | case 0:
|
||||||
72 | raise RuntimeError("boom!")
|
73 | raise RuntimeError("boom!")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ B904
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue