Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-pyi] Add autofix for PYI014 #3729

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind, Operator, Unaryop};

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

Expand All @@ -18,19 +18,23 @@ impl AlwaysAutofixableViolation for TypedArgumentDefaultInStub {
}

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

#[violation]
pub struct ArgumentDefaultInStub;

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

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

const ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS: &[&[&str]] = &[
Expand Down Expand Up @@ -270,9 +274,18 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
{
if arg.node.annotation.is_none() {
if !is_valid_default_value_with_annotation(default, checker, true) {
checker
.diagnostics
.push(Diagnostic::new(ArgumentDefaultInStub, Range::from(default)));
let mut diagnostic =
Diagnostic::new(ArgumentDefaultInStub, Range::from(default));

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

checker.diagnostics.push(diagnostic);
}
}
}
Expand All @@ -288,9 +301,18 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
{
if kwarg.node.annotation.is_none() {
if !is_valid_default_value_with_annotation(default, checker, true) {
checker
.diagnostics
.push(Diagnostic::new(ArgumentDefaultInStub, Range::from(default)));
let mut diagnostic =
Diagnostic::new(ArgumentDefaultInStub, Range::from(default));

if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Edit::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,7 +5,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 10
Expand All @@ -25,7 +25,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 38
Expand All @@ -45,7 +45,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 46
Expand All @@ -65,7 +65,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 63
Expand All @@ -85,7 +85,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 71
Expand All @@ -105,7 +105,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 78
Expand All @@ -125,7 +125,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 85
Expand All @@ -145,7 +145,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 90
Expand All @@ -165,7 +165,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 94
Expand All @@ -185,7 +185,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 98
Expand All @@ -205,7 +205,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 102
Expand All @@ -225,7 +225,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 106
Expand All @@ -245,7 +245,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 110
Expand All @@ -265,7 +265,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 138
Expand All @@ -285,7 +285,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 141
Expand All @@ -305,7 +305,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 147
Expand All @@ -325,7 +325,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 150
Expand All @@ -345,7 +345,7 @@ expression: diagnostics
- kind:
name: TypedArgumentDefaultInStub
body: Only simple default values allowed for typed arguments
suggestion: "Replace default value by `...`"
suggestion: "Replace default value with `...`"
fixable: true
location:
row: 159
Expand Down
Loading