Skip to content

Commit

Permalink
Add PYI011 auto-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Mar 13, 2023
1 parent 375039e commit 249dbe4
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 51 deletions.
25 changes: 19 additions & 6 deletions crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind, Operator, Unaryop};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::types::Range;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;

#[violation]
pub struct TypedArgumentSimpleDefaults;

/// PYI011
impl Violation for TypedArgumentSimpleDefaults {
impl AlwaysAutofixableViolation for TypedArgumentSimpleDefaults {
#[derive_message_formats]
fn message(&self) -> String {
format!("Only simple default values allowed for typed arguments")
}

fn autofix_title(&self) -> String {
"Replace default value by `...`".to_string()
}
}

#[violation]
Expand Down Expand Up @@ -188,10 +193,18 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
{
if arg.node.annotation.is_some() {
if !is_valid_default_value_with_annotation(default, checker) {
checker.diagnostics.push(Diagnostic::new(
TypedArgumentSimpleDefaults,
Range::from(default),
));
let mut diagnostic =
Diagnostic::new(TypedArgumentSimpleDefaults, Range::from(default));

if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
"...".to_string(),
default.location,
default.end_location.unwrap(),
));
}

checker.diagnostics.push(diagnostic);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,196 +5,301 @@ expression: diagnostics
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 10
column: 13
end_location:
row: 10
column: 23
fix: ~
fix:
content: "..."
location:
row: 10
column: 13
end_location:
row: 10
column: 23
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 16
column: 8
end_location:
row: 20
column: 5
fix: ~
fix:
content: "..."
location:
row: 16
column: 8
end_location:
row: 20
column: 5
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 25
column: 8
end_location:
row: 29
column: 5
fix: ~
fix:
content: "..."
location:
row: 25
column: 8
end_location:
row: 29
column: 5
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 34
column: 8
end_location:
row: 38
column: 5
fix: ~
fix:
content: "..."
location:
row: 34
column: 8
end_location:
row: 38
column: 5
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 43
column: 8
end_location:
row: 45
column: 5
fix: ~
fix:
content: "..."
location:
row: 43
column: 8
end_location:
row: 45
column: 5
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 48
column: 13
end_location:
row: 49
column: 11
fix: ~
fix:
content: "..."
location:
row: 48
column: 13
end_location:
row: 49
column: 11
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 52
column: 13
end_location:
row: 53
column: 12
fix: ~
fix:
content: "..."
location:
row: 52
column: 13
end_location:
row: 53
column: 12
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 56
column: 16
end_location:
row: 57
column: 7
fix: ~
fix:
content: "..."
location:
row: 56
column: 16
end_location:
row: 57
column: 7
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 60
column: 13
end_location:
row: 61
column: 7
fix: ~
fix:
content: "..."
location:
row: 60
column: 13
end_location:
row: 61
column: 7
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 64
column: 17
end_location:
row: 65
column: 8
fix: ~
fix:
content: "..."
location:
row: 64
column: 17
end_location:
row: 65
column: 8
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 68
column: 17
end_location:
row: 69
column: 10
fix: ~
fix:
content: "..."
location:
row: 68
column: 17
end_location:
row: 69
column: 10
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 96
column: 15
end_location:
row: 96
column: 18
fix: ~
fix:
content: "..."
location:
row: 96
column: 15
end_location:
row: 96
column: 18
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 99
column: 15
end_location:
row: 99
column: 21
fix: ~
fix:
content: "..."
location:
row: 99
column: 15
end_location:
row: 99
column: 21
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 105
column: 15
end_location:
row: 105
column: 24
fix: ~
fix:
content: "..."
location:
row: 105
column: 15
end_location:
row: 105
column: 24
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
suggestion: "Replace default value by `...`"
fixable: true
location:
row: 108
column: 17
end_location:
row: 109
column: 8
fix: ~
fix:
content: "..."
location:
row: 108
column: 17
end_location:
row: 109
column: 8
parent: ~

0 comments on commit 249dbe4

Please sign in to comment.