Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove autofix for prefer-type-error #2880

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI
| ---- | ---- | ------- | --- |
| TRY002 | [raise-vanilla-class](https://beta.ruff.rs/docs/rules/raise-vanilla-class/) | Create your own exception | |
| TRY003 | raise-vanilla-args | Avoid specifying long messages outside the exception class | |
| TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | 🛠 |
| TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | |
| TRY200 | reraise-no-cause | Use `raise from` to specify exception cause | |
| TRY201 | verbose-raise | Use `raise` without specifying exception name | |
| TRY300 | try-consider-else | Consider moving this statement to an `else` block | |
Expand Down
43 changes: 7 additions & 36 deletions crates/ruff/src/rules/tryceratops/rules/prefer_type_error.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};

use ruff_macros::{define_violation, derive_message_formats};

use crate::ast::types::Range;
use crate::ast::visitor;
use crate::ast::visitor::Visitor;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::violation::AlwaysAutofixableViolation;
use crate::violation::Violation;

define_violation!(
pub struct PreferTypeError;
);
impl AlwaysAutofixableViolation for PreferTypeError {
impl Violation for PreferTypeError {
#[derive_message_formats]
fn message(&self) -> String {
format!("Prefer `TypeError` exception for invalid type")
}

fn autofix_title(&self) -> String {
"Use `TypeError` exception type".to_string()
}
}

#[derive(Default)]
Expand Down Expand Up @@ -89,21 +85,6 @@ fn check_type_check_test(checker: &mut Checker, test: &Expr) -> bool {
}
}

/// Returns the [`Expr`] representing the name of the exception.
const fn match_name(exc: &Expr) -> Option<&Expr> {
match &exc.node {
ExprKind::Name { .. } => Some(exc),
ExprKind::Call { func, .. } => {
if let ExprKind::Name { .. } = &func.node {
Some(func)
} else {
None
}
}
_ => None,
}
}

/// Returns `true` if `exc` is a reference to a builtin exception.
fn is_builtin_exception(checker: &mut Checker, exc: &Expr) -> bool {
return checker.resolve_call_path(exc).map_or(false, |call_path| {
Expand Down Expand Up @@ -146,19 +127,9 @@ fn check_raise_type(checker: &mut Checker, exc: &Expr) -> bool {

fn check_raise(checker: &mut Checker, exc: &Expr, item: &Stmt) {
if check_raise_type(checker, exc) {
let mut diagnostic = Diagnostic::new(PreferTypeError, Range::from_located(item));

if checker.patch(diagnostic.kind.rule()) {
if let Some(name) = match_name(exc) {
diagnostic.amend(Fix::replacement(
"TypeError".to_string(),
name.location,
name.end_location.unwrap(),
));
}
}

checker.diagnostics.push(diagnostic);
checker
.diagnostics
.push(Diagnostic::new(PreferTypeError, Range::from_located(item)));
}
}

Expand Down
Loading