diff --git a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi index 8678a0958a..1a3c7b3c61 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi +++ b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi @@ -1,3 +1,10 @@ +import math +import os +import sys +from math import inf + +import numpy as np + def f12( x, y: str = os.pathsep, # Error PYI011 Only simple default values allowed for typed arguments @@ -101,3 +108,9 @@ def f35( x: complex = math.inf # Error PYI011 Only simple default values allowed for typed arguments + 1j, ) -> None: ... +def f36( + *, x: str = sys.version, # OK +) -> None: ... +def f37( + *, x: str = "" + "", # Error PYI011 Only simple default values allowed for typed arguments +) -> None: ... diff --git a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs index 8bafc22bdb..ba4ef5b062 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs @@ -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] @@ -111,7 +116,7 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) -> if let ExprKind::Attribute { .. } = &operand.node { if checker .ctx - .resolve_call_path(default) + .resolve_call_path(operand) .map_or(false, |call_path| { ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS.iter().any(|target| { // reject `-math.nan` @@ -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); } } } @@ -207,10 +220,18 @@ pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) { { if kwarg.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); } } } diff --git a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI011_PYI011.pyi.snap b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI011_PYI011.pyi.snap index 484cf00790..81f1c12f5a 100644 --- a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI011_PYI011.pyi.snap +++ b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI011_PYI011.pyi.snap @@ -5,248 +5,321 @@ 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: 3 + row: 10 column: 13 end_location: - row: 3 + 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: 9 + row: 16 column: 8 end_location: - row: 13 + 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: 18 + row: 25 column: 8 end_location: - row: 22 + 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: 27 - column: 8 - end_location: - row: 31 - column: 5 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 36 + 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: 41 - column: 13 + row: 43 + column: 8 end_location: - row: 42 - column: 11 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: row: 45 - column: 13 - end_location: - row: 46 - column: 12 - fix: ~ + column: 5 + 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: + 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: "Replace default value by `...`" + fixable: true + location: + row: 52 + column: 13 + end_location: + row: 53 + column: 12 + 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: "Replace default value by `...`" + fixable: true + location: + row: 56 column: 16 end_location: - row: 50 + 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: 53 + row: 60 column: 13 end_location: - row: 54 - column: 7 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 57 - column: 17 - end_location: - row: 58 - column: 8 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: row: 61 + column: 7 + 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: "Replace default value by `...`" + fixable: true + location: + row: 64 column: 17 end_location: - row: 62 - column: 10 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 80 - column: 15 - end_location: - row: 80 - column: 23 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 83 - column: 15 - end_location: - row: 83 - column: 23 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 86 - column: 15 - end_location: - row: 86 - column: 24 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 89 - column: 15 - end_location: - row: 89 - column: 18 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 92 - column: 15 - end_location: - row: 92 - column: 21 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 95 - column: 15 - end_location: - row: 95 - column: 23 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 98 - column: 15 - end_location: - row: 98 - column: 24 - fix: ~ - parent: ~ -- kind: - name: TypedArgumentSimpleDefaults - body: Only simple default values allowed for typed arguments - suggestion: ~ - fixable: false - location: - row: 101 - column: 17 - end_location: - row: 102 + 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: "Replace default value by `...`" + fixable: true + location: + row: 68 + column: 17 + end_location: + row: 69 + column: 10 + 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: "Replace default value by `...`" + fixable: true + location: + row: 96 + column: 15 + end_location: + row: 96 + column: 18 + 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: "Replace default value by `...`" + fixable: true + location: + row: 99 + column: 15 + end_location: + row: 99 + column: 21 + 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: "Replace default value by `...`" + fixable: true + location: + row: 105 + column: 15 + end_location: + row: 105 + column: 24 + 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: "Replace default value by `...`" + fixable: true + location: + row: 108 + column: 17 + end_location: + row: 109 + column: 8 + fix: + content: "..." + location: + row: 108 + column: 17 + end_location: + row: 109 + column: 8 + parent: ~ +- kind: + name: TypedArgumentSimpleDefaults + body: Only simple default values allowed for typed arguments + suggestion: "Replace default value by `...`" + fixable: true + location: + row: 115 + column: 16 + end_location: + row: 115 + column: 23 + fix: + content: "..." + location: + row: 115 + column: 16 + end_location: + row: 115 + column: 23 parent: ~