@@ -19,10 +19,13 @@ use crate::importer::ImportRequest;
19
19
/// constants were removed from the main namespace.
20
20
///
21
21
/// The majority of these functions and constants can be automatically replaced
22
- /// by other members of the NumPy API, even prior to NumPy 2.0, or by
23
- /// equivalents from the Python standard library. This rule flags all uses of
24
- /// removed members, along with automatic fixes for any backwards-compatible
25
- /// replacements.
22
+ /// by other members of the NumPy API or by equivalents from the Python
23
+ /// standard library. With the exception of renaming `numpy.byte_bounds` to
24
+ /// `numpy.lib.array_utils.byte_bounds`, all such replacements are backwards
25
+ /// compatible with earlier versions of NumPy.
26
+ ///
27
+ /// This rule flags all uses of removed members, along with automatic fixes for
28
+ /// any backwards-compatible replacements.
26
29
///
27
30
/// ## Examples
28
31
/// ```python
@@ -82,7 +85,11 @@ struct Replacement<'a> {
82
85
#[ derive( Debug ) ]
83
86
enum Details < ' a > {
84
87
/// The deprecated member can be replaced by another member in the NumPy API.
85
- AutoImport { path : & ' a str , name : & ' a str } ,
88
+ AutoImport {
89
+ path : & ' a str ,
90
+ name : & ' a str ,
91
+ compatibility : Compatibility ,
92
+ } ,
86
93
/// The deprecated member can be replaced by a member of the Python standard library.
87
94
AutoPurePython { python_expr : & ' a str } ,
88
95
/// The deprecated member can be replaced by a manual migration.
@@ -92,7 +99,18 @@ enum Details<'a> {
92
99
impl Details < ' _ > {
93
100
fn guideline ( & self ) -> Option < String > {
94
101
match self {
95
- Details :: AutoImport { path, name } => Some ( format ! ( "Use `{path}.{name}` instead." ) ) ,
102
+ Details :: AutoImport {
103
+ path,
104
+ name,
105
+ compatibility : Compatibility :: BackwardsCompatible ,
106
+ } => Some ( format ! ( "Use `{path}.{name}` instead." ) ) ,
107
+ Details :: AutoImport {
108
+ path,
109
+ name,
110
+ compatibility : Compatibility :: Breaking ,
111
+ } => Some ( format ! (
112
+ "Use `{path}.{name}` on NumPy 2.0, or ignore this warning on earlier versions."
113
+ ) ) ,
96
114
Details :: AutoPurePython { python_expr } => {
97
115
Some ( format ! ( "Use `{python_expr}` instead." ) )
98
116
}
@@ -101,6 +119,13 @@ impl Details<'_> {
101
119
}
102
120
}
103
121
122
+ #[ derive( Debug ) ]
123
+ enum Compatibility {
124
+ /// The changes is backwards compatible with earlier versions of NumPy.
125
+ BackwardsCompatible ,
126
+ /// The change is breaking in NumPy 2.0.
127
+ Breaking ,
128
+ }
104
129
/// NPY201
105
130
pub ( crate ) fn numpy_2_0_deprecation ( checker : & mut Checker , expr : & Expr ) {
106
131
let maybe_replacement = checker
@@ -113,13 +138,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
113
138
details : Details :: AutoImport {
114
139
path : "numpy.lib" ,
115
140
name : "add_docstring" ,
141
+ compatibility : Compatibility :: BackwardsCompatible ,
116
142
} ,
117
143
} ) ,
118
144
[ "numpy" , "add_newdoc" ] => Some ( Replacement {
119
145
existing : "add_newdoc" ,
120
146
details : Details :: AutoImport {
121
147
path : "numpy.lib" ,
122
148
name : "add_newdoc" ,
149
+ compatibility : Compatibility :: BackwardsCompatible ,
123
150
} ,
124
151
} ) ,
125
152
[ "numpy" , "add_newdoc_ufunc" ] => Some ( Replacement {
@@ -139,6 +166,7 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
139
166
details : Details :: AutoImport {
140
167
path : "numpy.lib.array_utils" ,
141
168
name : "byte_bounds" ,
169
+ compatibility : Compatibility :: Breaking ,
142
170
} ,
143
171
} ) ,
144
172
[ "numpy" , "cast" ] => Some ( Replacement {
@@ -152,13 +180,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
152
180
details : Details :: AutoImport {
153
181
path : "numpy" ,
154
182
name : "complex128" ,
183
+ compatibility : Compatibility :: BackwardsCompatible ,
155
184
} ,
156
185
} ) ,
157
186
[ "numpy" , "clongfloat" ] => Some ( Replacement {
158
187
existing : "clongfloat" ,
159
188
details : Details :: AutoImport {
160
189
path : "numpy" ,
161
190
name : "clongdouble" ,
191
+ compatibility : Compatibility :: BackwardsCompatible ,
162
192
} ,
163
193
} ) ,
164
194
[ "numpy" , "compat" ] => Some ( Replacement {
@@ -172,13 +202,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
172
202
details : Details :: AutoImport {
173
203
path : "numpy" ,
174
204
name : "complex128" ,
205
+ compatibility : Compatibility :: BackwardsCompatible ,
175
206
} ,
176
207
} ) ,
177
208
[ "numpy" , "DataSource" ] => Some ( Replacement {
178
209
existing : "DataSource" ,
179
210
details : Details :: AutoImport {
180
211
path : "numpy.lib.npyio" ,
181
212
name : "DataSource" ,
213
+ compatibility : Compatibility :: BackwardsCompatible ,
182
214
} ,
183
215
} ) ,
184
216
[ "numpy" , "deprecate" ] => Some ( Replacement {
@@ -222,6 +254,7 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
222
254
details : Details :: AutoImport {
223
255
path : "numpy" ,
224
256
name : "float64" ,
257
+ compatibility : Compatibility :: BackwardsCompatible ,
225
258
} ,
226
259
} ) ,
227
260
[ "numpy" , "geterrobj" ] => Some ( Replacement {
@@ -235,27 +268,31 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
235
268
details : Details :: AutoImport {
236
269
path : "numpy" ,
237
270
name : "inf" ,
271
+ compatibility : Compatibility :: BackwardsCompatible ,
238
272
} ,
239
273
} ) ,
240
274
[ "numpy" , "Inf" ] => Some ( Replacement {
241
275
existing : "Inf" ,
242
276
details : Details :: AutoImport {
243
277
path : "numpy" ,
244
278
name : "inf" ,
279
+ compatibility : Compatibility :: BackwardsCompatible ,
245
280
} ,
246
281
} ) ,
247
282
[ "numpy" , "Infinity" ] => Some ( Replacement {
248
283
existing : "Infinity" ,
249
284
details : Details :: AutoImport {
250
285
path : "numpy" ,
251
286
name : "inf" ,
287
+ compatibility : Compatibility :: BackwardsCompatible ,
252
288
} ,
253
289
} ) ,
254
290
[ "numpy" , "infty" ] => Some ( Replacement {
255
291
existing : "infty" ,
256
292
details : Details :: AutoImport {
257
293
path : "numpy" ,
258
294
name : "inf" ,
295
+ compatibility : Compatibility :: BackwardsCompatible ,
259
296
} ,
260
297
} ) ,
261
298
[ "numpy" , "issctype" ] => Some ( Replacement {
@@ -275,13 +312,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
275
312
details : Details :: AutoImport {
276
313
path : "numpy" ,
277
314
name : "issubdtype" ,
315
+ compatibility : Compatibility :: BackwardsCompatible ,
278
316
} ,
279
317
} ) ,
280
318
[ "numpy" , "mat" ] => Some ( Replacement {
281
319
existing : "mat" ,
282
320
details : Details :: AutoImport {
283
321
path : "numpy" ,
284
322
name : "asmatrix" ,
323
+ compatibility : Compatibility :: BackwardsCompatible ,
285
324
} ,
286
325
} ) ,
287
326
[ "numpy" , "maximum_sctype" ] => Some ( Replacement {
@@ -295,6 +334,7 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
295
334
details : Details :: AutoImport {
296
335
path : "numpy" ,
297
336
name : "nan" ,
337
+ compatibility : Compatibility :: BackwardsCompatible ,
298
338
} ,
299
339
} ) ,
300
340
[ "numpy" , "nbytes" ] => Some ( Replacement {
@@ -320,13 +360,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
320
360
details : Details :: AutoImport {
321
361
path : "numpy" ,
322
362
name : "clongdouble" ,
363
+ compatibility : Compatibility :: BackwardsCompatible ,
323
364
} ,
324
365
} ) ,
325
366
[ "numpy" , "longfloat" ] => Some ( Replacement {
326
367
existing : "longfloat" ,
327
368
details : Details :: AutoImport {
328
369
path : "numpy" ,
329
370
name : "longdouble" ,
371
+ compatibility : Compatibility :: BackwardsCompatible ,
330
372
} ,
331
373
} ) ,
332
374
[ "numpy" , "lookfor" ] => Some ( Replacement {
@@ -346,6 +388,7 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
346
388
details : Details :: AutoImport {
347
389
path : "numpy" ,
348
390
name : "inf" ,
391
+ compatibility : Compatibility :: BackwardsCompatible ,
349
392
} ,
350
393
} ) ,
351
394
[ "numpy" , "PZERO" ] => Some ( Replacement {
@@ -369,13 +412,15 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
369
412
details : Details :: AutoImport {
370
413
path : "numpy" ,
371
414
name : "round" ,
415
+ compatibility : Compatibility :: BackwardsCompatible ,
372
416
} ,
373
417
} ) ,
374
418
[ "numpy" , "safe_eval" ] => Some ( Replacement {
375
419
existing : "safe_eval" ,
376
420
details : Details :: AutoImport {
377
421
path : "ast" ,
378
422
name : "literal_eval" ,
423
+ compatibility : Compatibility :: BackwardsCompatible ,
379
424
} ,
380
425
} ) ,
381
426
[ "numpy" , "sctype2char" ] => Some ( Replacement {
@@ -407,34 +452,39 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
407
452
details : Details :: AutoImport {
408
453
path : "numpy" ,
409
454
name : "complex64" ,
455
+ compatibility : Compatibility :: BackwardsCompatible ,
410
456
} ,
411
457
} ) ,
412
458
[ "numpy" , "string_" ] => Some ( Replacement {
413
459
existing : "string_" ,
414
460
details : Details :: AutoImport {
415
461
path : "numpy" ,
416
462
name : "bytes_" ,
463
+ compatibility : Compatibility :: BackwardsCompatible ,
417
464
} ,
418
465
} ) ,
419
466
[ "numpy" , "source" ] => Some ( Replacement {
420
467
existing : "source" ,
421
468
details : Details :: AutoImport {
422
469
path : "inspect" ,
423
470
name : "getsource" ,
471
+ compatibility : Compatibility :: BackwardsCompatible ,
424
472
} ,
425
473
} ) ,
426
474
[ "numpy" , "tracemalloc_domain" ] => Some ( Replacement {
427
475
existing : "tracemalloc_domain" ,
428
476
details : Details :: AutoImport {
429
477
path : "numpy.lib" ,
430
478
name : "tracemalloc_domain" ,
479
+ compatibility : Compatibility :: BackwardsCompatible ,
431
480
} ,
432
481
} ) ,
433
482
[ "numpy" , "unicode_" ] => Some ( Replacement {
434
483
existing : "unicode_" ,
435
484
details : Details :: AutoImport {
436
485
path : "numpy" ,
437
486
name : "str_" ,
487
+ compatibility : Compatibility :: BackwardsCompatible ,
438
488
} ,
439
489
} ) ,
440
490
[ "numpy" , "who" ] => Some ( Replacement {
@@ -455,15 +505,26 @@ pub(crate) fn numpy_2_0_deprecation(checker: &mut Checker, expr: &Expr) {
455
505
expr. range ( ) ,
456
506
) ;
457
507
match replacement. details {
458
- Details :: AutoImport { path, name } => {
508
+ Details :: AutoImport {
509
+ path,
510
+ name,
511
+ compatibility,
512
+ } => {
459
513
diagnostic. try_set_fix ( || {
460
514
let ( import_edit, binding) = checker. importer ( ) . get_or_import_symbol (
461
515
& ImportRequest :: import_from ( path, name) ,
462
516
expr. start ( ) ,
463
517
checker. semantic ( ) ,
464
518
) ?;
465
519
let replacement_edit = Edit :: range_replacement ( binding, expr. range ( ) ) ;
466
- Ok ( Fix :: safe_edits ( import_edit, [ replacement_edit] ) )
520
+ Ok ( match compatibility {
521
+ Compatibility :: BackwardsCompatible => {
522
+ Fix :: safe_edits ( import_edit, [ replacement_edit] )
523
+ }
524
+ Compatibility :: Breaking => {
525
+ Fix :: unsafe_edits ( import_edit, [ replacement_edit] )
526
+ }
527
+ } )
467
528
} ) ;
468
529
}
469
530
Details :: AutoPurePython { python_expr } => diagnostic. set_fix ( Fix :: safe_edit (
0 commit comments