Skip to content

Commit

Permalink
Update NPY201 rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsokol committed Jun 27, 2024
1 parent 55f4812 commit 8adfff1
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 9 deletions.
24 changes: 24 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,27 @@ def func():
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.DTypePromotionError

np.ModuleDeprecationWarning

np.RankWarning

np.TooHardError

np.VisibleDeprecationWarning

np.compare_chararrays

np.chararray

np.format_parser
80 changes: 80 additions & 0 deletions crates/ruff_linter/src/rules/numpy/rules/numpy_2_0_deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,86 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "AxisError"] => Some(Replacement {
existing: "AxisError",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "AxisError",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "ComplexWarning"] => Some(Replacement {
existing: "ComplexWarning",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "ComplexWarning",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "DTypePromotionError"] => Some(Replacement {
existing: "DTypePromotionError",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "DTypePromotionError",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "ModuleDeprecationWarning"] => Some(Replacement {
existing: "ModuleDeprecationWarning",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "ModuleDeprecationWarning",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "RankWarning"] => Some(Replacement {
existing: "RankWarning",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "RankWarning",
compatibility: Compatibility::Breaking,
},
}),
["numpy", "TooHardError"] => Some(Replacement {
existing: "TooHardError",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "TooHardError",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "VisibleDeprecationWarning"] => Some(Replacement {
existing: "VisibleDeprecationWarning",
details: Details::AutoImport {
path: "numpy.exceptions",
name: "VisibleDeprecationWarning",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "compare_chararrays"] => Some(Replacement {
existing: "compare_chararrays",
details: Details::AutoImport {
path: "numpy.char",
name: "compare_chararrays",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "chararray"] => Some(Replacement {
existing: "chararray",
details: Details::AutoImport {
path: "numpy.char",
name: "chararray",
compatibility: Compatibility::BackwardsCompatible,
},
}),
["numpy", "format_parser"] => Some(Replacement {
existing: "format_parser",
details: Details::AutoImport {
path: "numpy.rec",
name: "format_parser",
compatibility: Compatibility::BackwardsCompatible,
},
}),
_ => None,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,20 +910,269 @@ NPY201.py:114:5: NPY201 [*] `np.cumproduct` will be removed in NumPy 2.0. Use `n
115 115 |
116 116 | np.product([1, 2, 3])

NPY201.py:116:5: NPY201 [*] `np.product` will be removed in NumPy 2.0. Use `numpy.prod` instead.
NPY201.py:122: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])
120 | np.cumproduct([1, 2, 3])
121 |
122 | np.product([1, 2, 3])
| ^^^^^^^^^^ NPY201
123 |
124 | np.trapz
|
= 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])
119 119 |
120 120 | np.cumproduct([1, 2, 3])
121 121 |
122 |- np.product([1, 2, 3])
122 |+ np.prod([1, 2, 3])
123 123 |
124 124 | np.trapz([1, 2, 3])
125 125 |

NPY201.py:124: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.
|
122 | np.product([1, 2, 3])
123 |
124 | np.trapz([1, 2, 3])
| ^^^^^^^^ NPY201
125 |
126 | np.in1d([1, 2], [1, 3, 5])
|
= help: Replace with `numpy.trapezoid` (requires NumPy 2.0 or greater)

ℹ Unsafe fix
121 121 |
122 122 | np.product([1, 2, 3])
123 123 |
124 |- np.trapz([1, 2, 3])
124 |+ np.trapezoid([1, 2, 3])
125 125 |
126 126 | np.in1d([1, 2], [1, 3, 5])
127 127 |

NPY201.py:126:5: NPY201 [*] `np.in1d` will be removed in NumPy 2.0. Use `numpy.isin` instead.
|
124 | np.trapz([1, 2, 3])
125 |
126 | np.in1d([1, 2], [1, 3, 5])
| ^^^^^^^ NPY201
127 |
128 | AxisError
|
= help: Replace with `numpy.isin`

ℹ Safe fix
123 123 |
124 124 | np.trapz([1, 2, 3])
125 125 |
126 |- np.in1d([1, 2], [1, 3, 5])
126 |+ np.isin([1, 2], [1, 3, 5])
127 127 |
128 128 | AxisError
129 129 |

NPY201.py:134:5: NPY201 [*] `np.ModuleDeprecationWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.ModuleDeprecationWarning` instead.
|
132 | DTypePromotionError
133 |
134 | np.ModuleDeprecationWarning
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ NPY201
135 |
136 | np.RankWarning
|
= help: Replace with `numpy.exceptions.ModuleDeprecationWarning`

ℹ Safe fix
3 3 | from numpy.lib.npyio import DataSource
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 |-from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
6 |+from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError, ModuleDeprecationWarning
7 7 | def func():
8 8 | import numpy as np
9 9 |
--------------------------------------------------------------------------------
131 131 |
132 132 | DTypePromotionError
133 133 |
134 |- np.ModuleDeprecationWarning
134 |+ ModuleDeprecationWarning
135 135 |
136 136 | np.RankWarning
137 137 |

NPY201.py:136: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.
|
134 | np.ModuleDeprecationWarning
135 |
136 | np.RankWarning
| ^^^^^^^^^^^^^^ NPY201
137 |
138 | np.TooHardError
|
= help: Replace with `numpy.exceptions.RankWarning` (requires NumPy 2.0 or greater)

ℹ Unsafe fix
3 3 | from numpy.lib.npyio import DataSource
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 |-from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
6 |+from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError, RankWarning
7 7 | def func():
8 8 | import numpy as np
9 9 |
--------------------------------------------------------------------------------
133 133 |
134 134 | np.ModuleDeprecationWarning
135 135 |
136 |- np.RankWarning
136 |+ RankWarning
137 137 |
138 138 | np.TooHardError
139 139 |

NPY201.py:138:5: NPY201 [*] `np.TooHardError` will be removed in NumPy 2.0. Use `numpy.exceptions.TooHardError` instead.
|
136 | np.RankWarning
137 |
138 | np.TooHardError
| ^^^^^^^^^^^^^^^ NPY201
139 |
140 | np.VisibleDeprecationWarning
|
= help: Replace with `numpy.exceptions.TooHardError`

ℹ Safe fix
3 3 | from numpy.lib.npyio import DataSource
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 |-from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
6 |+from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError, TooHardError
7 7 | def func():
8 8 | import numpy as np
9 9 |
--------------------------------------------------------------------------------
135 135 |
136 136 | np.RankWarning
137 137 |
138 |- np.TooHardError
138 |+ TooHardError
139 139 |
140 140 | np.VisibleDeprecationWarning
141 141 |

NPY201.py:140:5: NPY201 [*] `np.VisibleDeprecationWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.VisibleDeprecationWarning` instead.
|
138 | np.TooHardError
139 |
140 | np.VisibleDeprecationWarning
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ NPY201
141 |
142 | np.compare_chararrays
|
= help: Replace with `numpy.exceptions.VisibleDeprecationWarning`

ℹ Safe fix
3 3 | from numpy.lib.npyio import DataSource
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 |-from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
6 |+from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError, VisibleDeprecationWarning
7 7 | def func():
8 8 | import numpy as np
9 9 |
--------------------------------------------------------------------------------
137 137 |
138 138 | np.TooHardError
139 139 |
140 |- np.VisibleDeprecationWarning
140 |+ VisibleDeprecationWarning
141 141 |
142 142 | np.compare_chararrays
143 143 |

NPY201.py:142:5: NPY201 [*] `np.compare_chararrays` will be removed in NumPy 2.0. Use `numpy.char.compare_chararrays` instead.
|
140 | np.VisibleDeprecationWarning
141 |
142 | np.compare_chararrays
| ^^^^^^^^^^^^^^^^^^^^^ NPY201
143 |
144 | np.chararray
|
= help: Replace with `numpy.char.compare_chararrays`

ℹ Safe fix
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 6 | from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
7 |+from numpy.char import compare_chararrays
7 8 | def func():
8 9 | import numpy as np
9 10 |
--------------------------------------------------------------------------------
139 140 |
140 141 | np.VisibleDeprecationWarning
141 142 |
142 |- np.compare_chararrays
143 |+ compare_chararrays
143 144 |
144 145 | np.chararray
145 146 |

NPY201.py:144:5: NPY201 [*] `np.chararray` will be removed in NumPy 2.0. Use `numpy.char.chararray` instead.
|
142 | np.compare_chararrays
143 |
144 | np.chararray
| ^^^^^^^^^^^^ NPY201
145 |
146 | np.format_parser
|
= help: Replace with `numpy.char.chararray`

ℹ Safe fix
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 6 | from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
7 |+from numpy.char import chararray
7 8 | def func():
8 9 | import numpy as np
9 10 |
--------------------------------------------------------------------------------
141 142 |
142 143 | np.compare_chararrays
143 144 |
144 |- np.chararray
145 |+ chararray
145 146 |
146 147 | np.format_parser

NPY201.py:146:5: NPY201 [*] `np.format_parser` will be removed in NumPy 2.0. Use `numpy.rec.format_parser` instead.
|
144 | np.chararray
145 |
146 | np.format_parser
| ^^^^^^^^^^^^^^^^ NPY201
|
= help: Replace with `numpy.rec.format_parser`

ℹ Safe fix
4 4 | from ast import literal_eval
5 5 | from inspect import getsource
6 6 | from numpy.exceptions import AxisError, ComplexWarning, DTypePromotionError
7 |+from numpy.rec import format_parser
7 8 | def func():
8 9 | import numpy as np
9 10 |
--------------------------------------------------------------------------------
143 144 |
144 145 | np.chararray
145 146 |
146 |- np.format_parser
147 |+ format_parser


0 comments on commit 8adfff1

Please sign in to comment.