Skip to content

Commit 59ea94c

Browse files
authored
[numpy] Update NPY201 to include exception deprecations (#12065)
Hi! This PR updates `NPY201` rule to address #12034 and partially numpy/numpy#26800.
1 parent 5bef2b0 commit 59ea94c

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed

crates/ruff_linter/resources/test/fixtures/numpy/NPY201.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,13 @@ def func():
114114
np.cumproduct([1, 2, 3])
115115

116116
np.product([1, 2, 3])
117+
118+
np.trapz([1, 2, 3])
119+
120+
np.in1d([1, 2], [1, 3, 5])
121+
122+
np.AxisError
123+
124+
np.ComplexWarning
125+
126+
np.compare_chararrays

crates/ruff_linter/src/rules/numpy/rules/numpy_2_0_deprecation.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,86 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
574574
compatibility: Compatibility::BackwardsCompatible,
575575
},
576576
}),
577+
["numpy", "AxisError"] => Some(Replacement {
578+
existing: "AxisError",
579+
details: Details::AutoImport {
580+
path: "numpy.exceptions",
581+
name: "AxisError",
582+
compatibility: Compatibility::BackwardsCompatible,
583+
},
584+
}),
585+
["numpy", "ComplexWarning"] => Some(Replacement {
586+
existing: "ComplexWarning",
587+
details: Details::AutoImport {
588+
path: "numpy.exceptions",
589+
name: "ComplexWarning",
590+
compatibility: Compatibility::BackwardsCompatible,
591+
},
592+
}),
593+
["numpy", "DTypePromotionError"] => Some(Replacement {
594+
existing: "DTypePromotionError",
595+
details: Details::AutoImport {
596+
path: "numpy.exceptions",
597+
name: "DTypePromotionError",
598+
compatibility: Compatibility::BackwardsCompatible,
599+
},
600+
}),
601+
["numpy", "ModuleDeprecationWarning"] => Some(Replacement {
602+
existing: "ModuleDeprecationWarning",
603+
details: Details::AutoImport {
604+
path: "numpy.exceptions",
605+
name: "ModuleDeprecationWarning",
606+
compatibility: Compatibility::BackwardsCompatible,
607+
},
608+
}),
609+
["numpy", "RankWarning"] => Some(Replacement {
610+
existing: "RankWarning",
611+
details: Details::AutoImport {
612+
path: "numpy.exceptions",
613+
name: "RankWarning",
614+
compatibility: Compatibility::Breaking,
615+
},
616+
}),
617+
["numpy", "TooHardError"] => Some(Replacement {
618+
existing: "TooHardError",
619+
details: Details::AutoImport {
620+
path: "numpy.exceptions",
621+
name: "TooHardError",
622+
compatibility: Compatibility::BackwardsCompatible,
623+
},
624+
}),
625+
["numpy", "VisibleDeprecationWarning"] => Some(Replacement {
626+
existing: "VisibleDeprecationWarning",
627+
details: Details::AutoImport {
628+
path: "numpy.exceptions",
629+
name: "VisibleDeprecationWarning",
630+
compatibility: Compatibility::BackwardsCompatible,
631+
},
632+
}),
633+
["numpy", "compare_chararrays"] => Some(Replacement {
634+
existing: "compare_chararrays",
635+
details: Details::AutoImport {
636+
path: "numpy.char",
637+
name: "compare_chararrays",
638+
compatibility: Compatibility::BackwardsCompatible,
639+
},
640+
}),
641+
["numpy", "chararray"] => Some(Replacement {
642+
existing: "chararray",
643+
details: Details::AutoImport {
644+
path: "numpy.char",
645+
name: "chararray",
646+
compatibility: Compatibility::BackwardsCompatible,
647+
},
648+
}),
649+
["numpy", "format_parser"] => Some(Replacement {
650+
existing: "format_parser",
651+
details: Details::AutoImport {
652+
path: "numpy.rec",
653+
name: "format_parser",
654+
compatibility: Compatibility::BackwardsCompatible,
655+
},
656+
}),
577657
_ => None,
578658
});
579659

crates/ruff_linter/src/rules/numpy/snapshots/ruff_linter__rules__numpy__tests__numpy2-deprecation_NPY201.py.snap

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,16 @@ NPY201.py:114:5: NPY201 [*] `np.cumproduct` will be removed in NumPy 2.0. Use `n
909909
114 |+ np.cumprod([1, 2, 3])
910910
115 115 |
911911
116 116 | np.product([1, 2, 3])
912+
117 117 |
912913

913914
NPY201.py:116:5: NPY201 [*] `np.product` will be removed in NumPy 2.0. Use `numpy.prod` instead.
914915
|
915916
114 | np.cumproduct([1, 2, 3])
916917
115 |
917918
116 | np.product([1, 2, 3])
918919
| ^^^^^^^^^^ NPY201
920+
117 |
921+
118 | np.trapz([1, 2, 3])
919922
|
920923
= help: Replace with `numpy.prod`
921924

@@ -925,5 +928,120 @@ NPY201.py:116:5: NPY201 [*] `np.product` will be removed in NumPy 2.0. Use `nump
925928
115 115 |
926929
116 |- np.product([1, 2, 3])
927930
116 |+ np.prod([1, 2, 3])
931+
117 117 |
932+
118 118 | np.trapz([1, 2, 3])
933+
119 119 |
928934

935+
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.
936+
|
937+
116 | np.product([1, 2, 3])
938+
117 |
939+
118 | np.trapz([1, 2, 3])
940+
| ^^^^^^^^ NPY201
941+
119 |
942+
120 | np.in1d([1, 2], [1, 3, 5])
943+
|
944+
= help: Replace with `numpy.trapezoid` (requires NumPy 2.0 or greater)
945+
946+
ℹ Unsafe fix
947+
115 115 |
948+
116 116 | np.product([1, 2, 3])
949+
117 117 |
950+
118 |- np.trapz([1, 2, 3])
951+
118 |+ np.trapezoid([1, 2, 3])
952+
119 119 |
953+
120 120 | np.in1d([1, 2], [1, 3, 5])
954+
121 121 |
955+
956+
NPY201.py:120:5: NPY201 [*] `np.in1d` will be removed in NumPy 2.0. Use `numpy.isin` instead.
957+
|
958+
118 | np.trapz([1, 2, 3])
959+
119 |
960+
120 | np.in1d([1, 2], [1, 3, 5])
961+
| ^^^^^^^ NPY201
962+
121 |
963+
122 | np.AxisError
964+
|
965+
= help: Replace with `numpy.isin`
966+
967+
ℹ Safe fix
968+
117 117 |
969+
118 118 | np.trapz([1, 2, 3])
970+
119 119 |
971+
120 |- np.in1d([1, 2], [1, 3, 5])
972+
120 |+ np.isin([1, 2], [1, 3, 5])
973+
121 121 |
974+
122 122 | np.AxisError
975+
123 123 |
976+
977+
NPY201.py:122:5: NPY201 [*] `np.AxisError` will be removed in NumPy 2.0. Use `numpy.exceptions.AxisError` instead.
978+
|
979+
120 | np.in1d([1, 2], [1, 3, 5])
980+
121 |
981+
122 | np.AxisError
982+
| ^^^^^^^^^^^^ NPY201
983+
123 |
984+
124 | np.ComplexWarning
985+
|
986+
= help: Replace with `numpy.exceptions.AxisError`
987+
988+
ℹ Safe fix
989+
1 |+from numpy.exceptions import AxisError
990+
1 2 | def func():
991+
2 3 | import numpy as np
992+
3 4 |
993+
--------------------------------------------------------------------------------
994+
119 120 |
995+
120 121 | np.in1d([1, 2], [1, 3, 5])
996+
121 122 |
997+
122 |- np.AxisError
998+
123 |+ AxisError
999+
123 124 |
1000+
124 125 | np.ComplexWarning
1001+
125 126 |
1002+
1003+
NPY201.py:124:5: NPY201 [*] `np.ComplexWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.ComplexWarning` instead.
1004+
|
1005+
122 | np.AxisError
1006+
123 |
1007+
124 | np.ComplexWarning
1008+
| ^^^^^^^^^^^^^^^^^ NPY201
1009+
125 |
1010+
126 | np.compare_chararrays
1011+
|
1012+
= help: Replace with `numpy.exceptions.ComplexWarning`
1013+
1014+
ℹ Safe fix
1015+
1 |+from numpy.exceptions import ComplexWarning
1016+
1 2 | def func():
1017+
2 3 | import numpy as np
1018+
3 4 |
1019+
--------------------------------------------------------------------------------
1020+
121 122 |
1021+
122 123 | np.AxisError
1022+
123 124 |
1023+
124 |- np.ComplexWarning
1024+
125 |+ ComplexWarning
1025+
125 126 |
1026+
126 127 | np.compare_chararrays
1027+
1028+
NPY201.py:126:5: NPY201 [*] `np.compare_chararrays` will be removed in NumPy 2.0. Use `numpy.char.compare_chararrays` instead.
1029+
|
1030+
124 | np.ComplexWarning
1031+
125 |
1032+
126 | np.compare_chararrays
1033+
| ^^^^^^^^^^^^^^^^^^^^^ NPY201
1034+
|
1035+
= help: Replace with `numpy.char.compare_chararrays`
9291036

1037+
ℹ Safe fix
1038+
1 |+from numpy.char import compare_chararrays
1039+
1 2 | def func():
1040+
2 3 | import numpy as np
1041+
3 4 |
1042+
--------------------------------------------------------------------------------
1043+
123 124 |
1044+
124 125 | np.ComplexWarning
1045+
125 126 |
1046+
126 |- np.compare_chararrays
1047+
127 |+ compare_chararrays

crates/ruff_linter/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec<Message> {
9090
}
9191

9292
thread_local! {
93-
static MAX_ITERATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(10) };
93+
static MAX_ITERATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(12) };
9494
}
9595

9696
pub fn set_max_iterations(max: usize) {

0 commit comments

Comments
 (0)