Skip to content

Commit

Permalink
implement TRY400
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowake committed Jan 25, 2023
1 parent 0cac1a0 commit 592f9fa
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,12 +1209,13 @@ For more, see [flake8-type-checking](https://pypi.org/project/flake8-type-checki
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 | |
| TRY201 | verbose-raise | Use `raise` without specifying exception name | |
| TRY300 | try-consider-else | Consider `else` block | |
| TRY301 | raise-within-try | Abstract `raise` to an inner function | |
| TRY400 | error-instead-exception | Use logging `.exception` instead of `.error` | |

### flake8-use-pathlib (PTH)

Expand Down
20 changes: 20 additions & 0 deletions resources/test/fixtures/tryceratops/TRY400.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Violation:
Use '.exception' over '.error' inside except blocks
"""

import logging

logger = logging.getLogger(__name__)

def bad():
try:
a = 1
except Exception:
logger.error("Context message here")

def good():
try:
a = 1
except Exception:
logger.exception("Context message here")
3 changes: 3 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,9 @@
"TRY30",
"TRY300",
"TRY301",
"TRY4",
"TRY40",
"TRY400",
"TYP",
"TYP0",
"TYP00",
Expand Down
3 changes: 3 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,9 @@ where
if self.settings.rules.enabled(&Rule::RaiseWithinTry) {
tryceratops::rules::raise_within_try(self, body);
}
if self.settings.rules.enabled(&Rule::ErrorInsteadException) {
tryceratops::rules::error_instead_exception(self, handlers);
}
}
StmtKind::Assign { targets, value, .. } => {
if self.settings.rules.enabled(&Rule::DoNotAssignLambda) {
Expand Down
1 change: 1 addition & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ ruff_macros::define_rule_mapping!(
TRY201 => rules::tryceratops::rules::VerboseRaise,
TRY300 => rules::tryceratops::rules::TryConsiderElse,
TRY301 => rules::tryceratops::rules::RaiseWithinTry,
TRY400 => rules::tryceratops::rules::ErrorInsteadException,
// flake8-use-pathlib
PTH100 => rules::flake8_use_pathlib::violations::PathlibAbspath,
PTH101 => rules::flake8_use_pathlib::violations::PathlibChmod,
Expand Down
44 changes: 44 additions & 0 deletions src/rules/tryceratops/rules/exception_instead_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use ruff_macros::derive_message_formats;
use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind, 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 ErrorInsteadException;
);

impl Violation for ErrorInsteadException {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use logging '.exception' instead of '.error'")
}
}
fn visit_stmt(stmt: &Stmt) -> bool {
if let StmtKind::Expr { value, .. } = &stmt.node {
if let ExprKind::Call { func, .. } = &value.node {
if let ExprKind::Attribute { attr, .. } = &func.node {
return attr == &"error".to_string();
}
}
}
false
}

/// TRY400
pub fn error_instead_exception(checker: &mut Checker, handlers: &[Excepthandler]) {
for handler in handlers {
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node;
for stmt in body {
if visit_stmt(stmt) {
checker.diagnostics.push(Diagnostic::new(
ErrorInsteadException,
Range::from_located(stmt),
));
}
}
}
}
2 changes: 2 additions & 0 deletions src/rules/tryceratops/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub use exception_instead_error::{error_instead_exception, ErrorInsteadException};
pub use prefer_type_error::{prefer_type_error, PreferTypeError};
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 exception_instead_error;
mod prefer_type_error;
mod raise_within_try;
mod reraise_no_cause;
Expand Down

0 comments on commit 592f9fa

Please sign in to comment.