feat: enable autofix for TRY004 (#2084)

functionality was already implemented, just the trait needed to be added
This commit is contained in:
Simon Brugman 2023-01-22 13:18:56 +01:00 committed by GitHub
parent c1cb4796f8
commit 6fc6bf0648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -1194,7 +1194,7 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI
| Code | Name | Message | Fix | | Code | Name | Message | Fix |
| ---- | ---- | ------- | --- | | ---- | ---- | ------- | --- |
| TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | | | TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | 🛠 |
| TRY300 | try-consider-else | Consider `else` block | | | TRY300 | try-consider-else | Consider `else` block | |
### Ruff-specific rules (RUF) ### Ruff-specific rules (RUF)

View File

@ -6,16 +6,20 @@ use crate::checkers::ast::Checker;
use crate::define_violation; use crate::define_violation;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
use crate::violation::Violation; use crate::violation::AlwaysAutofixableViolation;
define_violation!( define_violation!(
pub struct PreferTypeError; pub struct PreferTypeError;
); );
impl Violation for PreferTypeError { impl AlwaysAutofixableViolation for PreferTypeError {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Prefer `TypeError` exception for invalid type") format!("Prefer `TypeError` exception for invalid type")
} }
fn autofix_title(&self) -> String {
"Use `TypeError` exception type".to_string()
}
} }
/// Returns `true` if an [`Expr`] is a call to check types. /// Returns `true` if an [`Expr`] is a call to check types.