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

Avoid attempting to fix NPY001 with overridden builtins #7187

Merged
merged 1 commit into from
Sep 6, 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
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/numpy/NPY001.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
)

_ = arr.astype(np.int)

# Regression test for: https://github.com/astral-sh/ruff/issues/6952
from numpy import float

float(1)
33 changes: 18 additions & 15 deletions crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ruff_python_ast::Expr;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::Expr;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -34,16 +33,18 @@ pub struct NumpyDeprecatedTypeAlias {
type_name: String,
}

impl AlwaysAutofixableViolation for NumpyDeprecatedTypeAlias {
impl Violation for NumpyDeprecatedTypeAlias {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
let NumpyDeprecatedTypeAlias { type_name } = self;
format!("Type alias `np.{type_name}` is deprecated, replace with builtin type")
}

fn autofix_title(&self) -> String {
fn autofix_title(&self) -> Option<String> {
let NumpyDeprecatedTypeAlias { type_name } = self;
format!("Replace `np.{type_name}` with builtin type")
Some(format!("Replace `np.{type_name}` with builtin type"))
}
}

Expand Down Expand Up @@ -73,15 +74,17 @@ pub(crate) fn deprecated_type_alias(checker: &mut Checker, expr: &Expr) {
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
match type_name {
"unicode" => "str",
"long" => "int",
_ => type_name,
}
.to_string(),
expr.range(),
)));
let type_name = match type_name {
"unicode" => "str",
"long" => "int",
_ => type_name,
};
if checker.semantic().is_builtin(type_name) {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
type_name.to_string(),
expr.range(),
)));
}
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ NPY001.py:20:16: NPY001 [*] Type alias `np.int` is deprecated, replace with buil
19 |
20 | _ = arr.astype(np.int)
| ^^^^^^ NPY001
21 |
22 | # Regression test for: https://github.com/astral-sh/ruff/issues/6952
|
= help: Replace `np.int` with builtin type

Expand All @@ -138,5 +140,17 @@ NPY001.py:20:16: NPY001 [*] Type alias `np.int` is deprecated, replace with buil
19 19 |
20 |-_ = arr.astype(np.int)
20 |+_ = arr.astype(int)
21 21 |
22 22 | # Regression test for: https://github.com/astral-sh/ruff/issues/6952
23 23 | from numpy import float

NPY001.py:25:1: NPY001 Type alias `np.float` is deprecated, replace with builtin type
|
23 | from numpy import float
24 |
25 | float(1)
| ^^^^^ NPY001
|
= help: Replace `np.float` with builtin type


Loading