|
| 1 | +use ruff_macros::{define_violation, derive_message_formats}; |
| 2 | +use rustpython_parser::ast::Expr; |
| 3 | + |
| 4 | +use crate::ast::types::Range; |
| 5 | +use crate::checkers::ast::Checker; |
| 6 | +use crate::fix::Fix; |
| 7 | +use crate::registry::Diagnostic; |
| 8 | +use crate::violation::AlwaysAutofixableViolation; |
| 9 | + |
| 10 | +define_violation!( |
| 11 | + /// ## What it does |
| 12 | + /// Checks for deprecated NumPy type aliases. |
| 13 | + /// |
| 14 | + /// ## Why is this bad? |
| 15 | + /// NumPy's `np.int` has long been an alias of the builtin `int`. The same |
| 16 | + /// goes for `np.float`, `np.bool`, and others. These aliases exist |
| 17 | + /// primarily primarily for historic reasons, and have been a cause of |
| 18 | + /// frequent confusion for newcomers. |
| 19 | + /// |
| 20 | + /// These aliases were been deprecated in 1.20, and removed in 1.24. |
| 21 | + /// |
| 22 | + /// ## Examples |
| 23 | + /// ```python |
| 24 | + /// import numpy as np |
| 25 | + /// |
| 26 | + /// np.bool |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// Use instead: |
| 30 | + /// ```python |
| 31 | + /// bool |
| 32 | + /// ``` |
| 33 | + pub struct NumpyDeprecatedTypeAlias { |
| 34 | + pub type_name: String, |
| 35 | + } |
| 36 | +); |
| 37 | +impl AlwaysAutofixableViolation for NumpyDeprecatedTypeAlias { |
| 38 | + #[derive_message_formats] |
| 39 | + fn message(&self) -> String { |
| 40 | + let NumpyDeprecatedTypeAlias { type_name } = self; |
| 41 | + format!("Type alias `np.{type_name}` is deprecated, replace with builtin type") |
| 42 | + } |
| 43 | + |
| 44 | + fn autofix_title(&self) -> String { |
| 45 | + let NumpyDeprecatedTypeAlias { type_name } = self; |
| 46 | + format!("Replace `np.{type_name}` with builtin type") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// NPY001 |
| 51 | +pub fn deprecated_type_alias(checker: &mut Checker, expr: &Expr) { |
| 52 | + if let Some(type_name) = checker.resolve_call_path(expr).and_then(|call_path| { |
| 53 | + if call_path.as_slice() == ["numpy", "bool"] |
| 54 | + || call_path.as_slice() == ["numpy", "int"] |
| 55 | + || call_path.as_slice() == ["numpy", "float"] |
| 56 | + || call_path.as_slice() == ["numpy", "complex"] |
| 57 | + || call_path.as_slice() == ["numpy", "object"] |
| 58 | + || call_path.as_slice() == ["numpy", "str"] |
| 59 | + || call_path.as_slice() == ["numpy", "long"] |
| 60 | + || call_path.as_slice() == ["numpy", "unicode"] |
| 61 | + { |
| 62 | + Some(call_path[1]) |
| 63 | + } else { |
| 64 | + None |
| 65 | + } |
| 66 | + }) { |
| 67 | + let mut diagnostic = Diagnostic::new( |
| 68 | + NumpyDeprecatedTypeAlias { |
| 69 | + type_name: type_name.to_string(), |
| 70 | + }, |
| 71 | + Range::from_located(expr), |
| 72 | + ); |
| 73 | + if checker.patch(diagnostic.kind.rule()) { |
| 74 | + diagnostic.amend(Fix::replacement( |
| 75 | + match type_name { |
| 76 | + "unicode" => "str", |
| 77 | + "long" => "int", |
| 78 | + _ => type_name, |
| 79 | + } |
| 80 | + .to_string(), |
| 81 | + expr.location, |
| 82 | + expr.end_location.unwrap(), |
| 83 | + )); |
| 84 | + } |
| 85 | + checker.diagnostics.push(diagnostic); |
| 86 | + } |
| 87 | +} |
0 commit comments