Skip to content

Commit

Permalink
feat: implement TRY002
Browse files Browse the repository at this point in the history
  • Loading branch information
karpa4o4 committed Jan 24, 2023
1 parent a204829 commit 2f881e3
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI

| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| TRY002 | raise-vanilla-class | Create your own exception | |
| 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 | |
Expand Down
11 changes: 11 additions & 0 deletions resources/test/fixtures/tryceratops/TRY002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MyException(Exception):
pass


def bad():
raise Exception()
raise Exception


def good():
raise MyException()
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,7 @@
"TRY",
"TRY0",
"TRY00",
"TRY002",
"TRY004",
"TRY2",
"TRY20",
Expand Down
5 changes: 5 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,11 @@ where
pyupgrade::rules::os_error_alias(self, &item);
}
}
if self.settings.rules.enabled(&Rule::RaiseVanillaClass) {
if let Some(expr) = exc {
tryceratops::rules::raise_vanilla_class(self, expr);
}
}
}
StmtKind::AugAssign { target, .. } => {
self.handle_node_load(target);
Expand Down
1 change: 1 addition & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ ruff_macros::define_rule_mapping!(
// flake8-type-checking
TYP005 => rules::flake8_type_checking::rules::EmptyTypeCheckingBlock,
// tryceratops
TRY002 => rules::tryceratops::rules::RaiseVanillaClass,
TRY004 => rules::tryceratops::rules::PreferTypeError,
TRY200 => rules::tryceratops::rules::ReraiseNoCause,
TRY201 => rules::tryceratops::rules::VerboseRaise,
Expand Down
1 change: 1 addition & 0 deletions src/rules/tryceratops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod tests {
use crate::registry::Rule;
use crate::settings;

#[test_case(Rule::RaiseVanillaClass, Path::new("TRY002.py"); "TRY002")]
#[test_case(Rule::PreferTypeError, Path::new("TRY004.py"); "TRY004")]
#[test_case(Rule::ReraiseNoCause, Path::new("TRY200.py"); "TRY200")]
#[test_case(Rule::VerboseRaise, Path::new("TRY201.py"); "TRY201")]
Expand Down
2 changes: 2 additions & 0 deletions src/rules/tryceratops/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub use prefer_type_error::{prefer_type_error, PreferTypeError};
pub use raise_vanilla_class::{raise_vanilla_class, RaiseVanillaClass};
pub use raise_within_try::{raise_within_try, RaiseWithinTry};
pub use reraise_no_cause::{reraise_no_cause, ReraiseNoCause};
pub use try_consider_else::{try_consider_else, TryConsiderElse};
pub use verbose_raise::{verbose_raise, VerboseRaise};

mod prefer_type_error;
mod raise_vanilla_class;
mod raise_within_try;
mod reraise_no_cause;
mod try_consider_else;
Expand Down
40 changes: 40 additions & 0 deletions src/rules/tryceratops/rules/raise_vanilla_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use ruff_macros::derive_message_formats;
use rustpython_ast::{Expr, ExprKind};

use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::define_violation;
use crate::registry::Diagnostic;
use crate::violation::Violation;

define_violation!(
pub struct RaiseVanillaClass;
);
impl Violation for RaiseVanillaClass {
#[derive_message_formats]
fn message(&self) -> String {
format!("Create your own exception")
}
}

fn get_name_expr(expr: &Expr) -> Option<&Expr> {
match &expr.node {
ExprKind::Call { func, .. } => get_name_expr(func),
ExprKind::Name { .. } => Some(expr),
_ => None,
}
}

/// TRY002
pub fn raise_vanilla_class(checker: &mut Checker, expr: &Expr) {
if let Some(func) = get_name_expr(expr) {
if let ExprKind::Name { id, .. } = &func.node {
if id == "Exception" {
checker.diagnostics.push(Diagnostic::new(
RaiseVanillaClass,
Range::from_located(expr),
));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/rules/tryceratops/mod.rs
expression: diagnostics
---
- kind:
RaiseVanillaClass: ~
location:
row: 6
column: 10
end_location:
row: 6
column: 21
fix: ~
parent: ~
- kind:
RaiseVanillaClass: ~
location:
row: 7
column: 10
end_location:
row: 7
column: 19
fix: ~
parent: ~

0 comments on commit 2f881e3

Please sign in to comment.