napari / napari

napari: a fast, interactive, multi-dimensional image viewer for python
https://napari.org
BSD 3-Clause "New" or "Revised" License
2.08k stars 411 forks source link

Add NumPy 2.0 linting #6752

Closed jni closed 2 weeks ago

jni commented 2 months ago

numpy 2.0b1 is out already. I'm not sure whether we're ready for it? Ruff has a linting rule for the transition:

https://numpy.org/devdocs/numpy_2_0_migration_guide.html#ruff-plugin

Our pre-release workflow has been failing for a long time 😅 so it does not provide us with a useful signal... 😢

GenevieveBuckley commented 2 months ago

Some positive news - there's not as many errors as you might expect! Only twelve errors, caused by two particular deprecations:

ruff check napari --select NPY201
napari/_qt/_tests/test_qt_viewer.py:973:18: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/_qt/_tests/test_qt_viewer.py:973:38: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/_qt/_tests/test_qt_viewer.py:999:31: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/_qt/_tests/test_qt_viewer.py:999:51: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/_qt/_tests/test_qt_viewer.py:1029:31: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/_qt/_tests/test_qt_viewer.py:1029:51: NPY201 `np.sctypes` will be removed without replacement in NumPy 2.0
napari/utils/_tracebacks.py:37:13: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
napari/utils/_tracebacks.py:54:13: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
napari/utils/_tracebacks.py:91:17: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
napari/utils/_tracebacks.py:128:17: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
napari/utils/_tracebacks.py:137:17: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
napari/utils/_tracebacks.py:143:17: NPY201 `np.set_string_function` will be removed in NumPy 2.0. Use `np.set_printoptions` for custom printing of NumPy objects.
Found 12 errors.
GenevieveBuckley commented 2 months ago

I've made a PR here: https://github.com/napari/napari/pull/6776

Need help: How do I replace np.set_string_function(lambda arr: f'{type(arr)} {arr.shape} {arr.dtype}') with an equivalent np.set_printoptions command? This is the numpy.set_printoptions docs, and these are the old numpy.set_string_function docs. I can change the formatting of individual numpy array elements, but I can't figure out how to make it return a single summary string for the whole array.