mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
Redirect TRY200
to B904
(#9755)
Follow-up to #9754 and #9689. Alternative to #9714. Marks `TRY200` as removed and redirects to `B904` instead of marking as deprecated and suggesting `B904` instead.
This commit is contained in:
parent
0d752e56cd
commit
a578414246
7 changed files with 5 additions and 93 deletions
|
@ -1,40 +0,0 @@
|
|||
"""
|
||||
Violation:
|
||||
Reraise without using 'from'
|
||||
"""
|
||||
|
||||
|
||||
class MyException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def func():
|
||||
try:
|
||||
a = 1
|
||||
except Exception:
|
||||
raise MyException()
|
||||
|
||||
|
||||
def func():
|
||||
try:
|
||||
a = 1
|
||||
except Exception:
|
||||
if True:
|
||||
raise MyException()
|
||||
|
||||
|
||||
def good():
|
||||
try:
|
||||
a = 1
|
||||
except MyException as e:
|
||||
raise e # This is verbose violation, shouldn't trigger no cause
|
||||
except Exception:
|
||||
raise # Just re-raising don't need 'from'
|
||||
|
||||
|
||||
def good():
|
||||
try:
|
||||
from mod import f
|
||||
except ImportError:
|
||||
def f():
|
||||
raise MyException() # Raising within a new scope is fine
|
|
@ -5,7 +5,6 @@ use crate::checkers::ast::Checker;
|
|||
use crate::registry::Rule;
|
||||
use crate::rules::{
|
||||
flake8_bandit, flake8_blind_except, flake8_bugbear, flake8_builtins, pycodestyle, pylint,
|
||||
tryceratops,
|
||||
};
|
||||
|
||||
/// Run lint rules over an [`ExceptHandler`] syntax node.
|
||||
|
@ -66,9 +65,6 @@ pub(crate) fn except_handler(except_handler: &ExceptHandler, checker: &mut Check
|
|||
if checker.enabled(Rule::ExceptWithNonExceptionClasses) {
|
||||
flake8_bugbear::rules::except_with_non_exception_classes(checker, except_handler);
|
||||
}
|
||||
if checker.enabled(Rule::ReraiseNoCause) {
|
||||
tryceratops::rules::reraise_no_cause(checker, body);
|
||||
}
|
||||
if checker.enabled(Rule::BinaryOpException) {
|
||||
pylint::rules::binary_op_exception(checker, except_handler);
|
||||
}
|
||||
|
|
|
@ -847,7 +847,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
|
|||
(Tryceratops, "002") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseVanillaClass),
|
||||
(Tryceratops, "003") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseVanillaArgs),
|
||||
(Tryceratops, "004") => (RuleGroup::Stable, rules::tryceratops::rules::TypeCheckWithoutTypeError),
|
||||
(Tryceratops, "200") => (RuleGroup::Deprecated, rules::tryceratops::rules::ReraiseNoCause),
|
||||
(Tryceratops, "200") => (RuleGroup::Removed, rules::tryceratops::rules::ReraiseNoCause),
|
||||
(Tryceratops, "201") => (RuleGroup::Stable, rules::tryceratops::rules::VerboseRaise),
|
||||
(Tryceratops, "300") => (RuleGroup::Stable, rules::tryceratops::rules::TryConsiderElse),
|
||||
(Tryceratops, "301") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseWithinTry),
|
||||
|
|
|
@ -100,6 +100,7 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
|||
("T004", "FIX004"),
|
||||
("RUF011", "B035"),
|
||||
("TCH006", "TCH010"),
|
||||
("TRY200", "B904"),
|
||||
// Test redirect by exact code
|
||||
#[cfg(feature = "test-rules")]
|
||||
("RUF940", "RUF950"),
|
||||
|
|
|
@ -18,7 +18,6 @@ mod tests {
|
|||
#[test_case(Rule::RaiseVanillaClass, Path::new("TRY002.py"))]
|
||||
#[test_case(Rule::RaiseVanillaArgs, Path::new("TRY003.py"))]
|
||||
#[test_case(Rule::TypeCheckWithoutTypeError, Path::new("TRY004.py"))]
|
||||
#[test_case(Rule::ReraiseNoCause, Path::new("TRY200.py"))]
|
||||
#[test_case(Rule::VerboseRaise, Path::new("TRY201.py"))]
|
||||
#[test_case(Rule::TryConsiderElse, Path::new("TRY300.py"))]
|
||||
#[test_case(Rule::RaiseWithinTry, Path::new("TRY301.py"))]
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
use ruff_python_ast::{Expr, Stmt};
|
||||
|
||||
use ruff_diagnostics::{Diagnostic, Violation};
|
||||
use ruff_diagnostics::Violation;
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::helpers::RaiseStatementVisitor;
|
||||
use ruff_python_ast::statement_visitor::StatementVisitor;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
||||
/// ## Deprecation
|
||||
/// ## Removed
|
||||
/// This rule is identical to [B904] which should be used instead.
|
||||
///
|
||||
/// ## What it does
|
||||
|
@ -44,28 +38,10 @@ use crate::checkers::ast::Checker;
|
|||
#[violation]
|
||||
pub struct ReraiseNoCause;
|
||||
|
||||
/// TRY200
|
||||
impl Violation for ReraiseNoCause {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("Use `raise from` to specify exception cause")
|
||||
}
|
||||
}
|
||||
|
||||
/// TRY200
|
||||
pub(crate) fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) {
|
||||
let raises = {
|
||||
let mut visitor = RaiseStatementVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.raises
|
||||
};
|
||||
|
||||
for (range, exc, cause) in raises {
|
||||
if cause.is_none() {
|
||||
if exc.is_some_and(Expr::is_call_expr) {
|
||||
checker
|
||||
.diagnostics
|
||||
.push(Diagnostic::new(ReraiseNoCause, range));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/tryceratops/mod.rs
|
||||
---
|
||||
TRY200.py:15:9: TRY200 Use `raise from` to specify exception cause
|
||||
|
|
||||
13 | a = 1
|
||||
14 | except Exception:
|
||||
15 | raise MyException()
|
||||
| ^^^^^^^^^^^^^^^^^^^ TRY200
|
||||
|
|
||||
|
||||
TRY200.py:23:13: TRY200 Use `raise from` to specify exception cause
|
||||
|
|
||||
21 | except Exception:
|
||||
22 | if True:
|
||||
23 | raise MyException()
|
||||
| ^^^^^^^^^^^^^^^^^^^ TRY200
|
||||
|
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue