Skip to content

Commit

Permalink
feat: implement TRY200
Browse files Browse the repository at this point in the history
  • Loading branch information
karpa4o4 committed Jan 22, 2023
1 parent 6fc6bf0 commit f10d4de
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,7 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | 🛠 |
| TRY200 | reraise-no-cause | Use `raise from` to specify exception cause | |
| TRY300 | try-consider-else | Consider `else` block | |

### Ruff-specific rules (RUF)
Expand Down
24 changes: 24 additions & 0 deletions resources/test/fixtures/tryceratops/TRY200.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MyException(Exception):
pass


class MainFunctionFailed(Exception):
pass


def process():
raise MyException


def bad():
try:
process()
except MyException:
raise MainFunctionFailed()


def good():
try:
process()
except MyException as ex:
raise MainFunctionFailed() from ex
3 changes: 3 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,9 @@
"TRY0",
"TRY00",
"TRY004",
"TRY2",
"TRY20",
"TRY200",
"TRY3",
"TRY30",
"TRY300",
Expand Down
3 changes: 3 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3306,6 +3306,9 @@ where
body,
);
}
if self.settings.rules.enabled(&Rule::ReraiseNoCause) {
tryceratops::rules::reraise_no_cause(self, body);
}
match name {
Some(name) => {
if self.settings.rules.enabled(&Rule::AmbiguousVariableName) {
Expand Down
1 change: 1 addition & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ ruff_macros::define_rule_mapping!(
TYP005 => rules::flake8_type_checking::rules::EmptyTypeCheckingBlock,
// tryceratops
TRY004 => rules::tryceratops::rules::PreferTypeError,
TRY200 => rules::tryceratops::rules::ReraiseNoCause,
TRY300 => rules::tryceratops::rules::TryConsiderElse,
// Ruff
RUF001 => violations::AmbiguousUnicodeCharacterString,
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 @@ -14,6 +14,7 @@ mod tests {
use crate::settings;

#[test_case(Rule::PreferTypeError, Path::new("TRY004.py"); "TRY004")]
#[test_case(Rule::ReraiseNoCause, Path::new("TRY200.py"); "TRY200")]
#[test_case(Rule::TryConsiderElse, Path::new("TRY300.py"); "TRY300")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy());
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,5 +1,7 @@
pub use prefer_type_error::{prefer_type_error, PreferTypeError};
pub use reraise_no_cause::{reraise_no_cause, ReraiseNoCause};
pub use try_consider_else::{try_consider_else, TryConsiderElse};

mod prefer_type_error;
mod reraise_no_cause;
mod try_consider_else;
32 changes: 32 additions & 0 deletions src/rules/tryceratops/rules/reraise_no_cause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ruff_macros::derive_message_formats;
use rustpython_ast::{Stmt, StmtKind};

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 ReraiseNoCause;
);
impl Violation for ReraiseNoCause {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use `raise from` to specify exception cause")
}
}

/// TRY200
pub fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) {
for stmt in body.iter() {
if let StmtKind::Raise { cause, .. } = &stmt.node {
if cause.is_none() {
checker.diagnostics.push(Diagnostic::new(
ReraiseNoCause,
Range::from_located(stmt),
));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: src/rules/tryceratops/mod.rs
expression: diagnostics
---
- kind:
ReraiseNoCause: ~
location:
row: 17
column: 8
end_location:
row: 17
column: 34
fix: ~
parent: ~

0 comments on commit f10d4de

Please sign in to comment.