Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
numpy
] deprecated type aliases (#2810)
Closes #2455 Used `NPY` as prefix code as agreed in the issue.
- Loading branch information
Showing
15 changed files
with
344 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
doc-valid-idents = ["StackOverflow", "CodeQL", "IPython", ".."] | ||
doc-valid-idents = [ | ||
"StackOverflow", | ||
"CodeQL", | ||
"IPython", | ||
"NumPy", | ||
"..", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import numpy as npy | ||
import numpy as np | ||
import numpy | ||
|
||
# Error | ||
npy.bool | ||
npy.int | ||
|
||
if dtype == np.object: | ||
... | ||
|
||
result = result.select_dtypes([np.byte, np.ubyte, np.short, np.ushort, np.int, np.long]) | ||
|
||
pdf = pd.DataFrame( | ||
data=[[1, 2, 3]], | ||
columns=["a", "b", "c"], | ||
dtype=numpy.object, | ||
) | ||
|
||
_ = arr.astype(np.int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! NumPy-specific rules. | ||
pub(crate) mod rules; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::convert::AsRef; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use test_case::test_case; | ||
|
||
use crate::registry::Rule; | ||
use crate::test::test_path; | ||
use crate::{assert_yaml_snapshot, settings}; | ||
|
||
#[test_case(Rule::NumpyDeprecatedTypeAlias, Path::new("NPY001.py"); "NPY001")] | ||
fn rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); | ||
let diagnostics = test_path( | ||
Path::new("numpy").join(path).as_path(), | ||
&settings::Settings::for_rule(rule_code), | ||
)?; | ||
assert_yaml_snapshot!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
crates/ruff/src/rules/numpy/rules/deprecated_type_alias.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use ruff_macros::{define_violation, derive_message_formats}; | ||
use rustpython_parser::ast::Expr; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::fix::Fix; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::AlwaysAutofixableViolation; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for deprecated NumPy type aliases. | ||
/// | ||
/// ## Why is this bad? | ||
/// NumPy's `np.int` has long been an alias of the builtin `int`. The same | ||
/// goes for `np.float`, `np.bool`, and others. These aliases exist | ||
/// primarily primarily for historic reasons, and have been a cause of | ||
/// frequent confusion for newcomers. | ||
/// | ||
/// These aliases were been deprecated in 1.20, and removed in 1.24. | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// import numpy as np | ||
/// | ||
/// np.bool | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// bool | ||
/// ``` | ||
pub struct NumpyDeprecatedTypeAlias { | ||
pub type_name: String, | ||
} | ||
); | ||
impl AlwaysAutofixableViolation for NumpyDeprecatedTypeAlias { | ||
#[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 { | ||
let NumpyDeprecatedTypeAlias { type_name } = self; | ||
format!("Replace `np.{type_name}` with builtin type") | ||
} | ||
} | ||
|
||
/// NPY001 | ||
pub fn deprecated_type_alias(checker: &mut Checker, expr: &Expr) { | ||
if let Some(type_name) = checker.resolve_call_path(expr).and_then(|call_path| { | ||
if call_path.as_slice() == ["numpy", "bool"] | ||
|| call_path.as_slice() == ["numpy", "int"] | ||
|| call_path.as_slice() == ["numpy", "float"] | ||
|| call_path.as_slice() == ["numpy", "complex"] | ||
|| call_path.as_slice() == ["numpy", "object"] | ||
|| call_path.as_slice() == ["numpy", "str"] | ||
|| call_path.as_slice() == ["numpy", "long"] | ||
|| call_path.as_slice() == ["numpy", "unicode"] | ||
{ | ||
Some(call_path[1]) | ||
} else { | ||
None | ||
} | ||
}) { | ||
let mut diagnostic = Diagnostic::new( | ||
NumpyDeprecatedTypeAlias { | ||
type_name: type_name.to_string(), | ||
}, | ||
Range::from_located(expr), | ||
); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.amend(Fix::replacement( | ||
match type_name { | ||
"unicode" => "str", | ||
"long" => "int", | ||
_ => type_name, | ||
} | ||
.to_string(), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub use deprecated_type_alias::{deprecated_type_alias, NumpyDeprecatedTypeAlias}; | ||
|
||
mod deprecated_type_alias; |
138 changes: 138 additions & 0 deletions
138
...les/numpy/snapshots/ruff__rules__numpy__tests__numpy-deprecated-type-alias_NPY001.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
--- | ||
source: crates/ruff/src/rules/numpy/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: bool | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 8 | ||
fix: | ||
content: | ||
- bool | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 8 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: int | ||
location: | ||
row: 7 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 7 | ||
fix: | ||
content: | ||
- int | ||
location: | ||
row: 7 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 7 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: object | ||
location: | ||
row: 9 | ||
column: 12 | ||
end_location: | ||
row: 9 | ||
column: 21 | ||
fix: | ||
content: | ||
- object | ||
location: | ||
row: 9 | ||
column: 12 | ||
end_location: | ||
row: 9 | ||
column: 21 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: int | ||
location: | ||
row: 12 | ||
column: 71 | ||
end_location: | ||
row: 12 | ||
column: 77 | ||
fix: | ||
content: | ||
- int | ||
location: | ||
row: 12 | ||
column: 71 | ||
end_location: | ||
row: 12 | ||
column: 77 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: long | ||
location: | ||
row: 12 | ||
column: 79 | ||
end_location: | ||
row: 12 | ||
column: 86 | ||
fix: | ||
content: | ||
- int | ||
location: | ||
row: 12 | ||
column: 79 | ||
end_location: | ||
row: 12 | ||
column: 86 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: object | ||
location: | ||
row: 17 | ||
column: 10 | ||
end_location: | ||
row: 17 | ||
column: 22 | ||
fix: | ||
content: | ||
- object | ||
location: | ||
row: 17 | ||
column: 10 | ||
end_location: | ||
row: 17 | ||
column: 22 | ||
parent: ~ | ||
- kind: | ||
NumpyDeprecatedTypeAlias: | ||
type_name: int | ||
location: | ||
row: 20 | ||
column: 15 | ||
end_location: | ||
row: 20 | ||
column: 21 | ||
fix: | ||
content: | ||
- int | ||
location: | ||
row: 20 | ||
column: 15 | ||
end_location: | ||
row: 20 | ||
column: 21 | ||
parent: ~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.