-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class BadArgCantBeEven(Exception): | ||
pass | ||
|
||
|
||
class GoodArgCantBeEven(Exception): | ||
def __init__(self, arg): | ||
super().__init__(f"The argument '{arg}' should be even") | ||
|
||
|
||
def bad(a): | ||
if a % 2 == 0: | ||
raise BadArgCantBeEven(f"The argument '{a}' should be even") | ||
|
||
|
||
def another_bad(a): | ||
if a % 2 == 0: | ||
raise BadArgCantBeEven(f"The argument {a} should not be odd.") | ||
|
||
|
||
def and_another_bad(a): | ||
if a % 2 == 0: | ||
raise BadArgCantBeEven('The argument `a` should not be odd.') | ||
|
||
|
||
def good(a: int): | ||
if a % 2 == 0: | ||
raise GoodArgCantBeEven(a) | ||
|
||
|
||
def another_good(a): | ||
if a % 2 == 0: | ||
raise GoodArgCantBeEven(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1769,6 +1769,7 @@ | |
"TRY0", | ||
"TRY00", | ||
"TRY002", | ||
"TRY003", | ||
"TRY004", | ||
"TRY2", | ||
"TRY20", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Constant, 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 RaiseVanillaArgs; | ||
); | ||
impl Violation for RaiseVanillaArgs { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Avoid specifying long messages outside the exception class") | ||
} | ||
} | ||
|
||
const WHITESPACE: &str = " "; | ||
|
||
fn get_string_arg_value(arg: &Expr) -> Option<String> { | ||
match &arg.node { | ||
ExprKind::JoinedStr { values } => { | ||
let value = values | ||
.iter() | ||
.map(|val| get_string_arg_value(val).unwrap_or_default()) | ||
.collect::<String>(); | ||
Some(value) | ||
} | ||
ExprKind::Constant { value, .. } => match value { | ||
Constant::Str(val) => Some(val.to_string()), | ||
_ => None, | ||
}, | ||
_ => None, | ||
} | ||
} | ||
|
||
/// TRY003 | ||
pub fn raise_vanilla_args(checker: &mut Checker, expr: &Expr) { | ||
if let ExprKind::Call { args, .. } = &expr.node { | ||
if let Some(arg_value) = get_string_arg_value(&args[0]) { | ||
if arg_value.contains(WHITESPACE) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(RaiseVanillaArgs, Range::from_located(expr))); | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../tryceratops/snapshots/ruff__rules__tryceratops__tests__raise-vanilla-args_TRY003.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
source: src/rules/tryceratops/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
RaiseVanillaArgs: ~ | ||
location: | ||
row: 12 | ||
column: 14 | ||
end_location: | ||
row: 12 | ||
column: 68 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RaiseVanillaArgs: ~ | ||
location: | ||
row: 17 | ||
column: 14 | ||
end_location: | ||
row: 17 | ||
column: 70 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RaiseVanillaArgs: ~ | ||
location: | ||
row: 22 | ||
column: 14 | ||
end_location: | ||
row: 22 | ||
column: 69 | ||
fix: ~ | ||
parent: ~ | ||
|