From c848712941cadd202b5170a63fda280e45e011d1 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 28 Jun 2024 09:41:16 +0200 Subject: [PATCH 1/3] Split NPY201 tests into two files to avoid exceeding the max iterations --- .../resources/test/fixtures/numpy/NPY201.py | 56 -- .../resources/test/fixtures/numpy/NPY201_2.py | 58 +++ crates/ruff_linter/src/rules/numpy/mod.rs | 1 + ...__tests__numpy2-deprecation_NPY201.py.snap | 485 ------------------ ...tests__numpy2-deprecation_NPY201_2.py.snap | 484 +++++++++++++++++ 5 files changed, 543 insertions(+), 541 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/numpy/NPY201_2.py create mode 100644 crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_2.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py index fe540e530e3ff..ec7108d176b44 100644 --- a/crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py +++ b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py @@ -68,59 +68,3 @@ def func(): np.longfloat(12+34j) np.lookfor - - np.obj2sctype(int) - - np.PINF - - np.PZERO - - np.recfromcsv - - np.recfromtxt - - np.round_(12.34) - - np.safe_eval - - np.sctype2char - - np.sctypes - - np.seterrobj - - np.set_numeric_ops - - np.set_string_function - - np.singlecomplex(12+1j) - - np.string_("asdf") - - np.source - - np.tracemalloc_domain - - np.unicode_("asf") - - np.who() - - np.row_stack(([1,2], [3,4])) - - np.alltrue([True, True]) - - np.anytrue([True, False]) - - np.cumproduct([1, 2, 3]) - - np.product([1, 2, 3]) - - np.trapz([1, 2, 3]) - - np.in1d([1, 2], [1, 3, 5]) - - np.AxisError - - np.ComplexWarning - - np.compare_chararrays diff --git a/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_2.py b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_2.py new file mode 100644 index 0000000000000..74f9afaa27259 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_2.py @@ -0,0 +1,58 @@ +def func(): + import numpy as np + + np.obj2sctype(int) + + np.PINF + + np.PZERO + + np.recfromcsv + + np.recfromtxt + + np.round_(12.34) + + np.safe_eval + + np.sctype2char + + np.sctypes + + np.seterrobj + + np.set_numeric_ops + + np.set_string_function + + np.singlecomplex(12+1j) + + np.string_("asdf") + + np.source + + np.tracemalloc_domain + + np.unicode_("asf") + + np.who() + + np.row_stack(([1,2], [3,4])) + + np.alltrue([True, True]) + + np.anytrue([True, False]) + + np.cumproduct([1, 2, 3]) + + np.product([1, 2, 3]) + + np.trapz([1, 2, 3]) + + np.in1d([1, 2], [1, 3, 5]) + + np.AxisError + + np.ComplexWarning + + np.compare_chararrays diff --git a/crates/ruff_linter/src/rules/numpy/mod.rs b/crates/ruff_linter/src/rules/numpy/mod.rs index b851107a322b3..f05c018437670 100644 --- a/crates/ruff_linter/src/rules/numpy/mod.rs +++ b/crates/ruff_linter/src/rules/numpy/mod.rs @@ -17,6 +17,7 @@ mod tests { #[test_case(Rule::NumpyLegacyRandom, Path::new("NPY002.py"))] #[test_case(Rule::NumpyDeprecatedFunction, Path::new("NPY003.py"))] #[test_case(Rule::Numpy2Deprecation, Path::new("NPY201.py"))] + #[test_case(Rule::Numpy2Deprecation, Path::new("NPY201_2.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201.py.snap b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201.py.snap index 0bfd1fbf08ac5..118febb550cfd 100644 --- a/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201.py.snap +++ b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201.py.snap @@ -552,7 +552,6 @@ NPY201.py:68:5: NPY201 [*] `np.longfloat` will be removed in NumPy 2.0. Use `num 68 |+ np.longdouble(12+34j) 69 69 | 70 70 | np.lookfor -71 71 | NPY201.py:70:5: NPY201 `np.lookfor` will be removed in NumPy 2.0. Search NumPy’s documentation directly. | @@ -560,488 +559,4 @@ NPY201.py:70:5: NPY201 `np.lookfor` will be removed in NumPy 2.0. Search NumPy 69 | 70 | np.lookfor | ^^^^^^^^^^ NPY201 -71 | -72 | np.obj2sctype(int) | - -NPY201.py:72:5: NPY201 `np.obj2sctype` will be removed without replacement in NumPy 2.0 - | -70 | np.lookfor -71 | -72 | np.obj2sctype(int) - | ^^^^^^^^^^^^^ NPY201 -73 | -74 | np.PINF - | - -NPY201.py:74:5: NPY201 [*] `np.PINF` will be removed in NumPy 2.0. Use `numpy.inf` instead. - | -72 | np.obj2sctype(int) -73 | -74 | np.PINF - | ^^^^^^^ NPY201 -75 | -76 | np.PZERO - | - = help: Replace with `numpy.inf` - -ℹ Safe fix -71 71 | -72 72 | np.obj2sctype(int) -73 73 | -74 |- np.PINF - 74 |+ np.inf -75 75 | -76 76 | np.PZERO -77 77 | - -NPY201.py:76:5: NPY201 [*] `np.PZERO` will be removed in NumPy 2.0. Use `0.0` instead. - | -74 | np.PINF -75 | -76 | np.PZERO - | ^^^^^^^^ NPY201 -77 | -78 | np.recfromcsv - | - = help: Replace with `0.0` - -ℹ Safe fix -73 73 | -74 74 | np.PINF -75 75 | -76 |- np.PZERO - 76 |+ 0.0 -77 77 | -78 78 | np.recfromcsv -79 79 | - -NPY201.py:78:5: NPY201 `np.recfromcsv` will be removed in NumPy 2.0. Use `np.genfromtxt` with comma delimiter instead. - | -76 | np.PZERO -77 | -78 | np.recfromcsv - | ^^^^^^^^^^^^^ NPY201 -79 | -80 | np.recfromtxt - | - -NPY201.py:80:5: NPY201 `np.recfromtxt` will be removed in NumPy 2.0. Use `np.genfromtxt` instead. - | -78 | np.recfromcsv -79 | -80 | np.recfromtxt - | ^^^^^^^^^^^^^ NPY201 -81 | -82 | np.round_(12.34) - | - -NPY201.py:82:5: NPY201 [*] `np.round_` will be removed in NumPy 2.0. Use `numpy.round` instead. - | -80 | np.recfromtxt -81 | -82 | np.round_(12.34) - | ^^^^^^^^^ NPY201 -83 | -84 | np.safe_eval - | - = help: Replace with `numpy.round` - -ℹ Safe fix -79 79 | -80 80 | np.recfromtxt -81 81 | -82 |- np.round_(12.34) - 82 |+ np.round(12.34) -83 83 | -84 84 | np.safe_eval -85 85 | - -NPY201.py:84:5: NPY201 [*] `np.safe_eval` will be removed in NumPy 2.0. Use `ast.literal_eval` instead. - | -82 | np.round_(12.34) -83 | -84 | np.safe_eval - | ^^^^^^^^^^^^ NPY201 -85 | -86 | np.sctype2char - | - = help: Replace with `ast.literal_eval` - -ℹ Safe fix - 1 |+from ast import literal_eval -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -81 82 | -82 83 | np.round_(12.34) -83 84 | -84 |- np.safe_eval - 85 |+ literal_eval -85 86 | -86 87 | np.sctype2char -87 88 | - -NPY201.py:86:5: NPY201 `np.sctype2char` will be removed without replacement in NumPy 2.0 - | -84 | np.safe_eval -85 | -86 | np.sctype2char - | ^^^^^^^^^^^^^^ NPY201 -87 | -88 | np.sctypes - | - -NPY201.py:88:5: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0 - | -86 | np.sctype2char -87 | -88 | np.sctypes - | ^^^^^^^^^^ NPY201 -89 | -90 | np.seterrobj - | - -NPY201.py:90:5: NPY201 `np.seterrobj` will be removed in NumPy 2.0. Use the `np.errstate` context manager instead. - | -88 | np.sctypes -89 | -90 | np.seterrobj - | ^^^^^^^^^^^^ NPY201 -91 | -92 | np.set_numeric_ops - | - -NPY201.py:94:5: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects. - | -92 | np.set_numeric_ops -93 | -94 | np.set_string_function - | ^^^^^^^^^^^^^^^^^^^^^^ NPY201 -95 | -96 | np.singlecomplex(12+1j) - | - -NPY201.py:96:5: NPY201 [*] `np.singlecomplex` will be removed in NumPy 2.0. Use `numpy.complex64` instead. - | -94 | np.set_string_function -95 | -96 | np.singlecomplex(12+1j) - | ^^^^^^^^^^^^^^^^ NPY201 -97 | -98 | np.string_("asdf") - | - = help: Replace with `numpy.complex64` - -ℹ Safe fix -93 93 | -94 94 | np.set_string_function -95 95 | -96 |- np.singlecomplex(12+1j) - 96 |+ np.complex64(12+1j) -97 97 | -98 98 | np.string_("asdf") -99 99 | - -NPY201.py:98:5: NPY201 [*] `np.string_` will be removed in NumPy 2.0. Use `numpy.bytes_` instead. - | - 96 | np.singlecomplex(12+1j) - 97 | - 98 | np.string_("asdf") - | ^^^^^^^^^^ NPY201 - 99 | -100 | np.source - | - = help: Replace with `numpy.bytes_` - -ℹ Safe fix -95 95 | -96 96 | np.singlecomplex(12+1j) -97 97 | -98 |- np.string_("asdf") - 98 |+ np.bytes_("asdf") -99 99 | -100 100 | np.source -101 101 | - -NPY201.py:100:5: NPY201 [*] `np.source` will be removed in NumPy 2.0. Use `inspect.getsource` instead. - | - 98 | np.string_("asdf") - 99 | -100 | np.source - | ^^^^^^^^^ NPY201 -101 | -102 | np.tracemalloc_domain - | - = help: Replace with `inspect.getsource` - -ℹ Safe fix - 1 |+from inspect import getsource -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -97 98 | -98 99 | np.string_("asdf") -99 100 | -100 |- np.source - 101 |+ getsource -101 102 | -102 103 | np.tracemalloc_domain -103 104 | - -NPY201.py:102:5: NPY201 [*] `np.tracemalloc_domain` will be removed in NumPy 2.0. Use `numpy.lib.tracemalloc_domain` instead. - | -100 | np.source -101 | -102 | np.tracemalloc_domain - | ^^^^^^^^^^^^^^^^^^^^^ NPY201 -103 | -104 | np.unicode_("asf") - | - = help: Replace with `numpy.lib.tracemalloc_domain` - -ℹ Safe fix - 1 |+from numpy.lib import tracemalloc_domain -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -99 100 | -100 101 | np.source -101 102 | -102 |- np.tracemalloc_domain - 103 |+ tracemalloc_domain -103 104 | -104 105 | np.unicode_("asf") -105 106 | - -NPY201.py:104:5: NPY201 [*] `np.unicode_` will be removed in NumPy 2.0. Use `numpy.str_` instead. - | -102 | np.tracemalloc_domain -103 | -104 | np.unicode_("asf") - | ^^^^^^^^^^^ NPY201 -105 | -106 | np.who() - | - = help: Replace with `numpy.str_` - -ℹ Safe fix -101 101 | -102 102 | np.tracemalloc_domain -103 103 | -104 |- np.unicode_("asf") - 104 |+ np.str_("asf") -105 105 | -106 106 | np.who() -107 107 | - -NPY201.py:106:5: NPY201 `np.who` will be removed in NumPy 2.0. Use an IDE variable explorer or `locals()` instead. - | -104 | np.unicode_("asf") -105 | -106 | np.who() - | ^^^^^^ NPY201 -107 | -108 | np.row_stack(([1,2], [3,4])) - | - -NPY201.py:108:5: NPY201 [*] `np.row_stack` will be removed in NumPy 2.0. Use `numpy.vstack` instead. - | -106 | np.who() -107 | -108 | np.row_stack(([1,2], [3,4])) - | ^^^^^^^^^^^^ NPY201 -109 | -110 | np.alltrue([True, True]) - | - = help: Replace with `numpy.vstack` - -ℹ Safe fix -105 105 | -106 106 | np.who() -107 107 | -108 |- np.row_stack(([1,2], [3,4])) - 108 |+ np.vstack(([1,2], [3,4])) -109 109 | -110 110 | np.alltrue([True, True]) -111 111 | - -NPY201.py:110:5: NPY201 [*] `np.alltrue` will be removed in NumPy 2.0. Use `all` instead. - | -108 | np.row_stack(([1,2], [3,4])) -109 | -110 | np.alltrue([True, True]) - | ^^^^^^^^^^ NPY201 -111 | -112 | np.anytrue([True, False]) - | - = help: Replace with `all` - -ℹ Safe fix -107 107 | -108 108 | np.row_stack(([1,2], [3,4])) -109 109 | -110 |- np.alltrue([True, True]) - 110 |+ all([True, True]) -111 111 | -112 112 | np.anytrue([True, False]) -113 113 | - -NPY201.py:114:5: NPY201 [*] `np.cumproduct` will be removed in NumPy 2.0. Use `numpy.cumprod` instead. - | -112 | np.anytrue([True, False]) -113 | -114 | np.cumproduct([1, 2, 3]) - | ^^^^^^^^^^^^^ NPY201 -115 | -116 | np.product([1, 2, 3]) - | - = help: Replace with `numpy.cumprod` - -ℹ Safe fix -111 111 | -112 112 | np.anytrue([True, False]) -113 113 | -114 |- np.cumproduct([1, 2, 3]) - 114 |+ np.cumprod([1, 2, 3]) -115 115 | -116 116 | np.product([1, 2, 3]) -117 117 | - -NPY201.py:116:5: NPY201 [*] `np.product` will be removed in NumPy 2.0. Use `numpy.prod` instead. - | -114 | np.cumproduct([1, 2, 3]) -115 | -116 | np.product([1, 2, 3]) - | ^^^^^^^^^^ NPY201 -117 | -118 | np.trapz([1, 2, 3]) - | - = help: Replace with `numpy.prod` - -ℹ Safe fix -113 113 | -114 114 | np.cumproduct([1, 2, 3]) -115 115 | -116 |- np.product([1, 2, 3]) - 116 |+ np.prod([1, 2, 3]) -117 117 | -118 118 | np.trapz([1, 2, 3]) -119 119 | - -NPY201.py:118:5: NPY201 [*] `np.trapz` will be removed in NumPy 2.0. Use `numpy.trapezoid` on NumPy 2.0, or ignore this warning on earlier versions. - | -116 | np.product([1, 2, 3]) -117 | -118 | np.trapz([1, 2, 3]) - | ^^^^^^^^ NPY201 -119 | -120 | np.in1d([1, 2], [1, 3, 5]) - | - = help: Replace with `numpy.trapezoid` (requires NumPy 2.0 or greater) - -ℹ Unsafe fix -115 115 | -116 116 | np.product([1, 2, 3]) -117 117 | -118 |- np.trapz([1, 2, 3]) - 118 |+ np.trapezoid([1, 2, 3]) -119 119 | -120 120 | np.in1d([1, 2], [1, 3, 5]) -121 121 | - -NPY201.py:120:5: NPY201 [*] `np.in1d` will be removed in NumPy 2.0. Use `numpy.isin` instead. - | -118 | np.trapz([1, 2, 3]) -119 | -120 | np.in1d([1, 2], [1, 3, 5]) - | ^^^^^^^ NPY201 -121 | -122 | np.AxisError - | - = help: Replace with `numpy.isin` - -ℹ Safe fix -117 117 | -118 118 | np.trapz([1, 2, 3]) -119 119 | -120 |- np.in1d([1, 2], [1, 3, 5]) - 120 |+ np.isin([1, 2], [1, 3, 5]) -121 121 | -122 122 | np.AxisError -123 123 | - -NPY201.py:122:5: NPY201 [*] `np.AxisError` will be removed in NumPy 2.0. Use `numpy.exceptions.AxisError` instead. - | -120 | np.in1d([1, 2], [1, 3, 5]) -121 | -122 | np.AxisError - | ^^^^^^^^^^^^ NPY201 -123 | -124 | np.ComplexWarning - | - = help: Replace with `numpy.exceptions.AxisError` - -ℹ Safe fix - 1 |+from numpy.exceptions import AxisError -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -119 120 | -120 121 | np.in1d([1, 2], [1, 3, 5]) -121 122 | -122 |- np.AxisError - 123 |+ AxisError -123 124 | -124 125 | np.ComplexWarning -125 126 | - -NPY201.py:124:5: NPY201 [*] `np.ComplexWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.ComplexWarning` instead. - | -122 | np.AxisError -123 | -124 | np.ComplexWarning - | ^^^^^^^^^^^^^^^^^ NPY201 -125 | -126 | np.compare_chararrays - | - = help: Replace with `numpy.exceptions.ComplexWarning` - -ℹ Safe fix - 1 |+from numpy.exceptions import ComplexWarning -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -121 122 | -122 123 | np.AxisError -123 124 | -124 |- np.ComplexWarning - 125 |+ ComplexWarning -125 126 | -126 127 | np.compare_chararrays - -NPY201.py:126:5: NPY201 [*] `np.compare_chararrays` will be removed in NumPy 2.0. Use `numpy.char.compare_chararrays` instead. - | -124 | np.ComplexWarning -125 | -126 | np.compare_chararrays - | ^^^^^^^^^^^^^^^^^^^^^ NPY201 - | - = help: Replace with `numpy.char.compare_chararrays` - -ℹ Safe fix - 1 |+from numpy.char import compare_chararrays -1 2 | def func(): -2 3 | import numpy as np -3 4 | --------------------------------------------------------------------------------- -123 124 | -124 125 | np.ComplexWarning -125 126 | -126 |- np.compare_chararrays - 127 |+ compare_chararrays diff --git a/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_2.py.snap b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_2.py.snap new file mode 100644 index 0000000000000..3bec0ccef7493 --- /dev/null +++ b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_2.py.snap @@ -0,0 +1,484 @@ +--- +source: crates/ruff_linter/src/rules/numpy/mod.rs +--- +NPY201_2.py:4:5: NPY201 `np.obj2sctype` will be removed without replacement in NumPy 2.0 + | +2 | import numpy as np +3 | +4 | np.obj2sctype(int) + | ^^^^^^^^^^^^^ NPY201 +5 | +6 | np.PINF + | + +NPY201_2.py:6:5: NPY201 [*] `np.PINF` will be removed in NumPy 2.0. Use `numpy.inf` instead. + | +4 | np.obj2sctype(int) +5 | +6 | np.PINF + | ^^^^^^^ NPY201 +7 | +8 | np.PZERO + | + = help: Replace with `numpy.inf` + +ℹ Safe fix +3 3 | +4 4 | np.obj2sctype(int) +5 5 | +6 |- np.PINF + 6 |+ np.inf +7 7 | +8 8 | np.PZERO +9 9 | + +NPY201_2.py:8:5: NPY201 [*] `np.PZERO` will be removed in NumPy 2.0. Use `0.0` instead. + | + 6 | np.PINF + 7 | + 8 | np.PZERO + | ^^^^^^^^ NPY201 + 9 | +10 | np.recfromcsv + | + = help: Replace with `0.0` + +ℹ Safe fix +5 5 | +6 6 | np.PINF +7 7 | +8 |- np.PZERO + 8 |+ 0.0 +9 9 | +10 10 | np.recfromcsv +11 11 | + +NPY201_2.py:10:5: NPY201 `np.recfromcsv` will be removed in NumPy 2.0. Use `np.genfromtxt` with comma delimiter instead. + | + 8 | np.PZERO + 9 | +10 | np.recfromcsv + | ^^^^^^^^^^^^^ NPY201 +11 | +12 | np.recfromtxt + | + +NPY201_2.py:12:5: NPY201 `np.recfromtxt` will be removed in NumPy 2.0. Use `np.genfromtxt` instead. + | +10 | np.recfromcsv +11 | +12 | np.recfromtxt + | ^^^^^^^^^^^^^ NPY201 +13 | +14 | np.round_(12.34) + | + +NPY201_2.py:14:5: NPY201 [*] `np.round_` will be removed in NumPy 2.0. Use `numpy.round` instead. + | +12 | np.recfromtxt +13 | +14 | np.round_(12.34) + | ^^^^^^^^^ NPY201 +15 | +16 | np.safe_eval + | + = help: Replace with `numpy.round` + +ℹ Safe fix +11 11 | +12 12 | np.recfromtxt +13 13 | +14 |- np.round_(12.34) + 14 |+ np.round(12.34) +15 15 | +16 16 | np.safe_eval +17 17 | + +NPY201_2.py:16:5: NPY201 [*] `np.safe_eval` will be removed in NumPy 2.0. Use `ast.literal_eval` instead. + | +14 | np.round_(12.34) +15 | +16 | np.safe_eval + | ^^^^^^^^^^^^ NPY201 +17 | +18 | np.sctype2char + | + = help: Replace with `ast.literal_eval` + +ℹ Safe fix + 1 |+from ast import literal_eval +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +13 14 | +14 15 | np.round_(12.34) +15 16 | +16 |- np.safe_eval + 17 |+ literal_eval +17 18 | +18 19 | np.sctype2char +19 20 | + +NPY201_2.py:18:5: NPY201 `np.sctype2char` will be removed without replacement in NumPy 2.0 + | +16 | np.safe_eval +17 | +18 | np.sctype2char + | ^^^^^^^^^^^^^^ NPY201 +19 | +20 | np.sctypes + | + +NPY201_2.py:20:5: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0 + | +18 | np.sctype2char +19 | +20 | np.sctypes + | ^^^^^^^^^^ NPY201 +21 | +22 | np.seterrobj + | + +NPY201_2.py:22:5: NPY201 `np.seterrobj` will be removed in NumPy 2.0. Use the `np.errstate` context manager instead. + | +20 | np.sctypes +21 | +22 | np.seterrobj + | ^^^^^^^^^^^^ NPY201 +23 | +24 | np.set_numeric_ops + | + +NPY201_2.py:26:5: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects. + | +24 | np.set_numeric_ops +25 | +26 | np.set_string_function + | ^^^^^^^^^^^^^^^^^^^^^^ NPY201 +27 | +28 | np.singlecomplex(12+1j) + | + +NPY201_2.py:28:5: NPY201 [*] `np.singlecomplex` will be removed in NumPy 2.0. Use `numpy.complex64` instead. + | +26 | np.set_string_function +27 | +28 | np.singlecomplex(12+1j) + | ^^^^^^^^^^^^^^^^ NPY201 +29 | +30 | np.string_("asdf") + | + = help: Replace with `numpy.complex64` + +ℹ Safe fix +25 25 | +26 26 | np.set_string_function +27 27 | +28 |- np.singlecomplex(12+1j) + 28 |+ np.complex64(12+1j) +29 29 | +30 30 | np.string_("asdf") +31 31 | + +NPY201_2.py:30:5: NPY201 [*] `np.string_` will be removed in NumPy 2.0. Use `numpy.bytes_` instead. + | +28 | np.singlecomplex(12+1j) +29 | +30 | np.string_("asdf") + | ^^^^^^^^^^ NPY201 +31 | +32 | np.source + | + = help: Replace with `numpy.bytes_` + +ℹ Safe fix +27 27 | +28 28 | np.singlecomplex(12+1j) +29 29 | +30 |- np.string_("asdf") + 30 |+ np.bytes_("asdf") +31 31 | +32 32 | np.source +33 33 | + +NPY201_2.py:32:5: NPY201 [*] `np.source` will be removed in NumPy 2.0. Use `inspect.getsource` instead. + | +30 | np.string_("asdf") +31 | +32 | np.source + | ^^^^^^^^^ NPY201 +33 | +34 | np.tracemalloc_domain + | + = help: Replace with `inspect.getsource` + +ℹ Safe fix + 1 |+from inspect import getsource +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +29 30 | +30 31 | np.string_("asdf") +31 32 | +32 |- np.source + 33 |+ getsource +33 34 | +34 35 | np.tracemalloc_domain +35 36 | + +NPY201_2.py:34:5: NPY201 [*] `np.tracemalloc_domain` will be removed in NumPy 2.0. Use `numpy.lib.tracemalloc_domain` instead. + | +32 | np.source +33 | +34 | np.tracemalloc_domain + | ^^^^^^^^^^^^^^^^^^^^^ NPY201 +35 | +36 | np.unicode_("asf") + | + = help: Replace with `numpy.lib.tracemalloc_domain` + +ℹ Safe fix + 1 |+from numpy.lib import tracemalloc_domain +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +31 32 | +32 33 | np.source +33 34 | +34 |- np.tracemalloc_domain + 35 |+ tracemalloc_domain +35 36 | +36 37 | np.unicode_("asf") +37 38 | + +NPY201_2.py:36:5: NPY201 [*] `np.unicode_` will be removed in NumPy 2.0. Use `numpy.str_` instead. + | +34 | np.tracemalloc_domain +35 | +36 | np.unicode_("asf") + | ^^^^^^^^^^^ NPY201 +37 | +38 | np.who() + | + = help: Replace with `numpy.str_` + +ℹ Safe fix +33 33 | +34 34 | np.tracemalloc_domain +35 35 | +36 |- np.unicode_("asf") + 36 |+ np.str_("asf") +37 37 | +38 38 | np.who() +39 39 | + +NPY201_2.py:38:5: NPY201 `np.who` will be removed in NumPy 2.0. Use an IDE variable explorer or `locals()` instead. + | +36 | np.unicode_("asf") +37 | +38 | np.who() + | ^^^^^^ NPY201 +39 | +40 | np.row_stack(([1,2], [3,4])) + | + +NPY201_2.py:40:5: NPY201 [*] `np.row_stack` will be removed in NumPy 2.0. Use `numpy.vstack` instead. + | +38 | np.who() +39 | +40 | np.row_stack(([1,2], [3,4])) + | ^^^^^^^^^^^^ NPY201 +41 | +42 | np.alltrue([True, True]) + | + = help: Replace with `numpy.vstack` + +ℹ Safe fix +37 37 | +38 38 | np.who() +39 39 | +40 |- np.row_stack(([1,2], [3,4])) + 40 |+ np.vstack(([1,2], [3,4])) +41 41 | +42 42 | np.alltrue([True, True]) +43 43 | + +NPY201_2.py:42:5: NPY201 [*] `np.alltrue` will be removed in NumPy 2.0. Use `all` instead. + | +40 | np.row_stack(([1,2], [3,4])) +41 | +42 | np.alltrue([True, True]) + | ^^^^^^^^^^ NPY201 +43 | +44 | np.anytrue([True, False]) + | + = help: Replace with `all` + +ℹ Safe fix +39 39 | +40 40 | np.row_stack(([1,2], [3,4])) +41 41 | +42 |- np.alltrue([True, True]) + 42 |+ all([True, True]) +43 43 | +44 44 | np.anytrue([True, False]) +45 45 | + +NPY201_2.py:46:5: NPY201 [*] `np.cumproduct` will be removed in NumPy 2.0. Use `numpy.cumprod` instead. + | +44 | np.anytrue([True, False]) +45 | +46 | np.cumproduct([1, 2, 3]) + | ^^^^^^^^^^^^^ NPY201 +47 | +48 | np.product([1, 2, 3]) + | + = help: Replace with `numpy.cumprod` + +ℹ Safe fix +43 43 | +44 44 | np.anytrue([True, False]) +45 45 | +46 |- np.cumproduct([1, 2, 3]) + 46 |+ np.cumprod([1, 2, 3]) +47 47 | +48 48 | np.product([1, 2, 3]) +49 49 | + +NPY201_2.py:48:5: NPY201 [*] `np.product` will be removed in NumPy 2.0. Use `numpy.prod` instead. + | +46 | np.cumproduct([1, 2, 3]) +47 | +48 | np.product([1, 2, 3]) + | ^^^^^^^^^^ NPY201 +49 | +50 | np.trapz([1, 2, 3]) + | + = help: Replace with `numpy.prod` + +ℹ Safe fix +45 45 | +46 46 | np.cumproduct([1, 2, 3]) +47 47 | +48 |- np.product([1, 2, 3]) + 48 |+ np.prod([1, 2, 3]) +49 49 | +50 50 | np.trapz([1, 2, 3]) +51 51 | + +NPY201_2.py:50:5: NPY201 [*] `np.trapz` will be removed in NumPy 2.0. Use `numpy.trapezoid` on NumPy 2.0, or ignore this warning on earlier versions. + | +48 | np.product([1, 2, 3]) +49 | +50 | np.trapz([1, 2, 3]) + | ^^^^^^^^ NPY201 +51 | +52 | np.in1d([1, 2], [1, 3, 5]) + | + = help: Replace with `numpy.trapezoid` (requires NumPy 2.0 or greater) + +ℹ Unsafe fix +47 47 | +48 48 | np.product([1, 2, 3]) +49 49 | +50 |- np.trapz([1, 2, 3]) + 50 |+ np.trapezoid([1, 2, 3]) +51 51 | +52 52 | np.in1d([1, 2], [1, 3, 5]) +53 53 | + +NPY201_2.py:52:5: NPY201 [*] `np.in1d` will be removed in NumPy 2.0. Use `numpy.isin` instead. + | +50 | np.trapz([1, 2, 3]) +51 | +52 | np.in1d([1, 2], [1, 3, 5]) + | ^^^^^^^ NPY201 +53 | +54 | np.AxisError + | + = help: Replace with `numpy.isin` + +ℹ Safe fix +49 49 | +50 50 | np.trapz([1, 2, 3]) +51 51 | +52 |- np.in1d([1, 2], [1, 3, 5]) + 52 |+ np.isin([1, 2], [1, 3, 5]) +53 53 | +54 54 | np.AxisError +55 55 | + +NPY201_2.py:54:5: NPY201 [*] `np.AxisError` will be removed in NumPy 2.0. Use `numpy.exceptions.AxisError` instead. + | +52 | np.in1d([1, 2], [1, 3, 5]) +53 | +54 | np.AxisError + | ^^^^^^^^^^^^ NPY201 +55 | +56 | np.ComplexWarning + | + = help: Replace with `numpy.exceptions.AxisError` + +ℹ Safe fix + 1 |+from numpy.exceptions import AxisError +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +51 52 | +52 53 | np.in1d([1, 2], [1, 3, 5]) +53 54 | +54 |- np.AxisError + 55 |+ AxisError +55 56 | +56 57 | np.ComplexWarning +57 58 | + +NPY201_2.py:56:5: NPY201 [*] `np.ComplexWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.ComplexWarning` instead. + | +54 | np.AxisError +55 | +56 | np.ComplexWarning + | ^^^^^^^^^^^^^^^^^ NPY201 +57 | +58 | np.compare_chararrays + | + = help: Replace with `numpy.exceptions.ComplexWarning` + +ℹ Safe fix + 1 |+from numpy.exceptions import ComplexWarning +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +53 54 | +54 55 | np.AxisError +55 56 | +56 |- np.ComplexWarning + 57 |+ ComplexWarning +57 58 | +58 59 | np.compare_chararrays + +NPY201_2.py:58:5: NPY201 [*] `np.compare_chararrays` will be removed in NumPy 2.0. Use `numpy.char.compare_chararrays` instead. + | +56 | np.ComplexWarning +57 | +58 | np.compare_chararrays + | ^^^^^^^^^^^^^^^^^^^^^ NPY201 + | + = help: Replace with `numpy.char.compare_chararrays` + +ℹ Safe fix + 1 |+from numpy.char import compare_chararrays +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +55 56 | +56 57 | np.ComplexWarning +57 58 | +58 |- np.compare_chararrays + 59 |+ compare_chararrays From 0420ed1ab6d818db2ca12a8c6debfdf524f9cf8e Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 28 Jun 2024 09:44:30 +0200 Subject: [PATCH 2/3] More NPY201 tests --- .../resources/test/fixtures/numpy/NPY201_3.py | 16 ++ crates/ruff_linter/src/rules/numpy/mod.rs | 2 + ...tests__numpy2-deprecation_NPY201_3.py.snap | 172 ++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 crates/ruff_linter/resources/test/fixtures/numpy/NPY201_3.py create mode 100644 crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_3.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_3.py b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_3.py new file mode 100644 index 0000000000000..2f01375301cea --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/numpy/NPY201_3.py @@ -0,0 +1,16 @@ +def func(): + import numpy as np + + np.DTypePromotionError + + np.ModuleDeprecationWarning + + np.RankWarning + + np.TooHardError + + np.VisibleDeprecationWarning + + np.chararray + + np.format_parser diff --git a/crates/ruff_linter/src/rules/numpy/mod.rs b/crates/ruff_linter/src/rules/numpy/mod.rs index f05c018437670..fdf65ba09ef48 100644 --- a/crates/ruff_linter/src/rules/numpy/mod.rs +++ b/crates/ruff_linter/src/rules/numpy/mod.rs @@ -16,8 +16,10 @@ mod tests { #[test_case(Rule::NumpyDeprecatedTypeAlias, Path::new("NPY001.py"))] #[test_case(Rule::NumpyLegacyRandom, Path::new("NPY002.py"))] #[test_case(Rule::NumpyDeprecatedFunction, Path::new("NPY003.py"))] + // The NPY201 tests are split into multiple files because they get fixed one by one and too many diagnostic exceed the max-iterations limit. #[test_case(Rule::Numpy2Deprecation, Path::new("NPY201.py"))] #[test_case(Rule::Numpy2Deprecation, Path::new("NPY201_2.py"))] + #[test_case(Rule::Numpy2Deprecation, Path::new("NPY201_3.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_3.py.snap b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_3.py.snap new file mode 100644 index 0000000000000..2ba8e85fa30d1 --- /dev/null +++ b/crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201_3.py.snap @@ -0,0 +1,172 @@ +--- +source: crates/ruff_linter/src/rules/numpy/mod.rs +--- +NPY201_3.py:4:5: NPY201 [*] `np.DTypePromotionError` will be removed in NumPy 2.0. Use `numpy.exceptions.DTypePromotionError` instead. + | +2 | import numpy as np +3 | +4 | np.DTypePromotionError + | ^^^^^^^^^^^^^^^^^^^^^^ NPY201 +5 | +6 | np.ModuleDeprecationWarning + | + = help: Replace with `numpy.exceptions.DTypePromotionError` + +ℹ Safe fix + 1 |+from numpy.exceptions import DTypePromotionError +1 2 | def func(): +2 3 | import numpy as np +3 4 | +4 |- np.DTypePromotionError + 5 |+ DTypePromotionError +5 6 | +6 7 | np.ModuleDeprecationWarning +7 8 | + +NPY201_3.py:6:5: NPY201 [*] `np.ModuleDeprecationWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.ModuleDeprecationWarning` instead. + | +4 | np.DTypePromotionError +5 | +6 | np.ModuleDeprecationWarning + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ NPY201 +7 | +8 | np.RankWarning + | + = help: Replace with `numpy.exceptions.ModuleDeprecationWarning` + +ℹ Safe fix + 1 |+from numpy.exceptions import ModuleDeprecationWarning +1 2 | def func(): +2 3 | import numpy as np +3 4 | +4 5 | np.DTypePromotionError +5 6 | +6 |- np.ModuleDeprecationWarning + 7 |+ ModuleDeprecationWarning +7 8 | +8 9 | np.RankWarning +9 10 | + +NPY201_3.py:8:5: NPY201 [*] `np.RankWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.RankWarning` on NumPy 2.0, or ignore this warning on earlier versions. + | + 6 | np.ModuleDeprecationWarning + 7 | + 8 | np.RankWarning + | ^^^^^^^^^^^^^^ NPY201 + 9 | +10 | np.TooHardError + | + = help: Replace with `numpy.exceptions.RankWarning` (requires NumPy 2.0 or greater) + +ℹ Unsafe fix + 1 |+from numpy.exceptions import RankWarning +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +5 6 | +6 7 | np.ModuleDeprecationWarning +7 8 | +8 |- np.RankWarning + 9 |+ RankWarning +9 10 | +10 11 | np.TooHardError +11 12 | + +NPY201_3.py:10:5: NPY201 [*] `np.TooHardError` will be removed in NumPy 2.0. Use `numpy.exceptions.TooHardError` instead. + | + 8 | np.RankWarning + 9 | +10 | np.TooHardError + | ^^^^^^^^^^^^^^^ NPY201 +11 | +12 | np.VisibleDeprecationWarning + | + = help: Replace with `numpy.exceptions.TooHardError` + +ℹ Safe fix + 1 |+from numpy.exceptions import TooHardError +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +7 8 | +8 9 | np.RankWarning +9 10 | +10 |- np.TooHardError + 11 |+ TooHardError +11 12 | +12 13 | np.VisibleDeprecationWarning +13 14 | + +NPY201_3.py:12:5: NPY201 [*] `np.VisibleDeprecationWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.VisibleDeprecationWarning` instead. + | +10 | np.TooHardError +11 | +12 | np.VisibleDeprecationWarning + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ NPY201 +13 | +14 | np.chararray + | + = help: Replace with `numpy.exceptions.VisibleDeprecationWarning` + +ℹ Safe fix + 1 |+from numpy.exceptions import VisibleDeprecationWarning +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +9 10 | +10 11 | np.TooHardError +11 12 | +12 |- np.VisibleDeprecationWarning + 13 |+ VisibleDeprecationWarning +13 14 | +14 15 | np.chararray +15 16 | + +NPY201_3.py:14:5: NPY201 [*] `np.chararray` will be removed in NumPy 2.0. Use `numpy.char.chararray` instead. + | +12 | np.VisibleDeprecationWarning +13 | +14 | np.chararray + | ^^^^^^^^^^^^ NPY201 +15 | +16 | np.format_parser + | + = help: Replace with `numpy.char.chararray` + +ℹ Safe fix + 1 |+from numpy.char import chararray +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +11 12 | +12 13 | np.VisibleDeprecationWarning +13 14 | +14 |- np.chararray + 15 |+ chararray +15 16 | +16 17 | np.format_parser + +NPY201_3.py:16:5: NPY201 [*] `np.format_parser` will be removed in NumPy 2.0. Use `numpy.rec.format_parser` instead. + | +14 | np.chararray +15 | +16 | np.format_parser + | ^^^^^^^^^^^^^^^^ NPY201 + | + = help: Replace with `numpy.rec.format_parser` + +ℹ Safe fix + 1 |+from numpy.rec import format_parser +1 2 | def func(): +2 3 | import numpy as np +3 4 | +-------------------------------------------------------------------------------- +13 14 | +14 15 | np.chararray +15 16 | +16 |- np.format_parser + 17 |+ format_parser From a130abc836c7c8e14a22f0a26a0af28dce55cdfe Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 28 Jun 2024 09:45:29 +0200 Subject: [PATCH 3/3] Reduce the max iterations counter back to 10 --- crates/ruff_linter/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/test.rs b/crates/ruff_linter/src/test.rs index 9653c1d9da55f..55a259ff4fe90 100644 --- a/crates/ruff_linter/src/test.rs +++ b/crates/ruff_linter/src/test.rs @@ -90,7 +90,7 @@ pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec { } thread_local! { - static MAX_ITERATIONS: std::cell::Cell = const { std::cell::Cell::new(12) }; + static MAX_ITERATIONS: std::cell::Cell = const { std::cell::Cell::new(10) }; } pub fn set_max_iterations(max: usize) {