Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NumPy 2.0 deprecations via running ruff #2580

Merged
merged 2 commits into from
Aug 21, 2024

Conversation

radoering
Copy link
Contributor

@radoering radoering commented Jul 19, 2024

I just run the following command:

ruff check --select NPY201 --fix --exclude docs/sphinx/rest_substitutions/snippets/python/converted

to fix numpy deprecations that were removed and thereby result in an exception with numpy 2.0.

See https://numpy.org/devdocs/numpy_2_0_migration_guide.html#numpy-2-0-migration-guide

Fixes #2574
Fixes #2498
Closes #2578

wx/lib/floatcanvas/FloatCanvas.py Outdated Show resolved Hide resolved
wx/lib/plot/plotcanvas.py Outdated Show resolved Hide resolved
@newville
Copy link
Contributor

newville commented Aug 5, 2024

Please merge.

@swt2c swt2c merged commit f7d1d81 into wxWidgets:master Aug 21, 2024
21 checks passed
@bnavigator
Copy link
Contributor

The unit tests have been missed by this PR:

[ 1232s] =================================== FAILURES ===================================
[ 1232s] __________________________ testFromPoints.testCreate ___________________________
[ 1232s] [gw0] linux -- Python 3.10.14 /usr/bin/python3.10
[ 1232s] self = <unittests.test_lib_floatcanvas_bbox.testFromPoints testMethod=testCreate>
[ 1232s] 
[ 1232s]     def testCreate(self):
[ 1232s]         Pts = N.array( ((5,2),
[ 1232s]                 (3,4),
[ 1232s]                 (1,6),
[ 1232s] >               ), N.float_ )
[ 1232s] 
[ 1232s] unittests/test_lib_floatcanvas_bbox.py:344: 
[ 1232s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
[ 1232s] 
[ 1232s] attr = 'float_'
[ 1232s] 
[ 1232s]     def __getattr__(attr):
[ 1232s]         # Warn for expired attributes
[ 1232s]         import warnings
[ 1232s]     
[ 1232s]         if attr == "linalg":
[ 1232s]             import numpy.linalg as linalg
[ 1232s]             return linalg
[ 1232s]         elif attr == "fft":
[ 1232s]             import numpy.fft as fft
[ 1232s]             return fft
[ 1232s]         elif attr == "dtypes":
[ 1232s]             import numpy.dtypes as dtypes
[ 1232s]             return dtypes
[ 1232s]         elif attr == "random":
[ 1232s]             import numpy.random as random
[ 1232s]             return random
[ 1232s]         elif attr == "polynomial":
[ 1232s]             import numpy.polynomial as polynomial
[ 1232s]             return polynomial
[ 1232s]         elif attr == "ma":
[ 1232s]             import numpy.ma as ma
[ 1232s]             return ma
[ 1232s]         elif attr == "ctypeslib":
[ 1232s]             import numpy.ctypeslib as ctypeslib
[ 1232s]             return ctypeslib
[ 1232s]         elif attr == "exceptions":
[ 1232s]             import numpy.exceptions as exceptions
[ 1232s]             return exceptions
[ 1232s]         elif attr == "testing":
[ 1232s]             import numpy.testing as testing
[ 1232s]             return testing
[ 1232s]         elif attr == "matlib":
[ 1232s]             import numpy.matlib as matlib
[ 1232s]             return matlib
[ 1232s]         elif attr == "f2py":
[ 1232s]             import numpy.f2py as f2py
[ 1232s]             return f2py
[ 1232s]         elif attr == "typing":
[ 1232s]             import numpy.typing as typing
[ 1232s]             return typing
[ 1232s]         elif attr == "rec":
[ 1232s]             import numpy.rec as rec
[ 1232s]             return rec
[ 1232s]         elif attr == "char":
[ 1232s]             import numpy.char as char
[ 1232s]             return char
[ 1232s]         elif attr == "array_api":
[ 1232s]             raise AttributeError("`numpy.array_api` is not available from "
[ 1232s]                                  "numpy 2.0 onwards")
[ 1232s]         elif attr == "core":
[ 1232s]             import numpy.core as core
[ 1232s]             return core
[ 1232s]         elif attr == "strings":
[ 1232s]             import numpy.strings as strings
[ 1232s]             return strings
[ 1232s]         elif attr == "distutils":
[ 1232s]             if 'distutils' in __numpy_submodules__:
[ 1232s]                 import numpy.distutils as distutils
[ 1232s]                 return distutils
[ 1232s]             else:
[ 1232s]                 raise AttributeError("`numpy.distutils` is not available from "
[ 1232s]                                      "Python 3.12 onwards")
[ 1232s]     
[ 1232s]         if attr in __future_scalars__:
[ 1232s]             # And future warnings for those that will change, but also give
[ 1232s]             # the AttributeError
[ 1232s]             warnings.warn(
[ 1232s]                 f"In the future `np.{attr}` will be defined as the "
[ 1232s]                 "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
[ 1232s]     
[ 1232s]         if attr in __former_attrs__:
[ 1232s]             raise AttributeError(__former_attrs__[attr])
[ 1232s]     
[ 1232s]         if attr in __expired_attributes__:
[ 1232s] >           raise AttributeError(
[ 1232s]                 f"`np.{attr}` was removed in the NumPy 2.0 release. "
[ 1232s]                 f"{__expired_attributes__[attr]}"
[ 1232s]             )
[ 1232s] E           AttributeError: `np.float_` was removed in the NumPy 2.0 release. Use `np.float64` instead.. Did you mean: 'float16'?
[ 1232s] 
[ 1232s] /usr/lib64/python3.10/site-packages/numpy/__init__.py:397: AttributeError
[ 1232s] ___________________________ testNullBBox.testValues ____________________________
[ 1232s] [gw0] linux -- Python 3.10.14 /usr/bin/python3.10
[ 1232s] self = <unittests.test_lib_floatcanvas_bbox.testNullBBox testMethod=testValues>
[ 1232s] 
[ 1232s]     def testValues(self):
[ 1232s] >       self.assertTrue( N.alltrue(N.isnan(self.B1)) )
[ 1232s] 
[ 1232s] unittests/test_lib_floatcanvas_bbox.py:463: 
[ 1232s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
[ 1232s] 
[ 1232s] attr = 'alltrue'
[ 1232s] 
[ 1232s]     def __getattr__(attr):
[ 1232s]         # Warn for expired attributes
[ 1232s]         import warnings
[ 1232s]     
[ 1232s]         if attr == "linalg":
[ 1232s]             import numpy.linalg as linalg
[ 1232s]             return linalg
[ 1232s]         elif attr == "fft":
[ 1232s]             import numpy.fft as fft
[ 1232s]             return fft
[ 1232s]         elif attr == "dtypes":
[ 1232s]             import numpy.dtypes as dtypes
[ 1232s]             return dtypes
[ 1232s]         elif attr == "random":
[ 1232s]             import numpy.random as random
[ 1232s]             return random
[ 1232s]         elif attr == "polynomial":
[ 1232s]             import numpy.polynomial as polynomial
[ 1232s]             return polynomial
[ 1232s]         elif attr == "ma":
[ 1232s]             import numpy.ma as ma
[ 1232s]             return ma
[ 1232s]         elif attr == "ctypeslib":
[ 1232s]             import numpy.ctypeslib as ctypeslib
[ 1232s]             return ctypeslib
[ 1232s]         elif attr == "exceptions":
[ 1232s]             import numpy.exceptions as exceptions
[ 1232s]             return exceptions
[ 1232s]         elif attr == "testing":
[ 1232s]             import numpy.testing as testing
[ 1232s]             return testing
[ 1232s]         elif attr == "matlib":
[ 1232s]             import numpy.matlib as matlib
[ 1232s]             return matlib
[ 1232s]         elif attr == "f2py":
[ 1232s]             import numpy.f2py as f2py
[ 1232s]             return f2py
[ 1232s]         elif attr == "typing":
[ 1232s]             import numpy.typing as typing
[ 1232s]             return typing
[ 1232s]         elif attr == "rec":
[ 1232s]             import numpy.rec as rec
[ 1232s]             return rec
[ 1232s]         elif attr == "char":
[ 1232s]             import numpy.char as char
[ 1232s]             return char
[ 1232s]         elif attr == "array_api":
[ 1232s]             raise AttributeError("`numpy.array_api` is not available from "
[ 1232s]                                  "numpy 2.0 onwards")
[ 1232s]         elif attr == "core":
[ 1232s]             import numpy.core as core
[ 1232s]             return core
[ 1232s]         elif attr == "strings":
[ 1232s]             import numpy.strings as strings
[ 1232s]             return strings
[ 1232s]         elif attr == "distutils":
[ 1232s]             if 'distutils' in __numpy_submodules__:
[ 1232s]                 import numpy.distutils as distutils
[ 1232s]                 return distutils
[ 1232s]             else:
[ 1232s]                 raise AttributeError("`numpy.distutils` is not available from "
[ 1232s]                                      "Python 3.12 onwards")
[ 1232s]     
[ 1232s]         if attr in __future_scalars__:
[ 1232s]             # And future warnings for those that will change, but also give
[ 1232s]             # the AttributeError
[ 1232s]             warnings.warn(
[ 1232s]                 f"In the future `np.{attr}` will be defined as the "
[ 1232s]                 "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
[ 1232s]     
[ 1232s]         if attr in __former_attrs__:
[ 1232s]             raise AttributeError(__former_attrs__[attr])
[ 1232s]     
[ 1232s]         if attr in __expired_attributes__:
[ 1232s]             raise AttributeError(
[ 1232s]                 f"`np.{attr}` was removed in the NumPy 2.0 release. "
[ 1232s]                 f"{__expired_attributes__[attr]}"
[ 1232s]             )
[ 1232s]     
[ 1232s]         if attr == "chararray":
[ 1232s]             warnings.warn(
[ 1232s]                 "`np.chararray` is deprecated and will be removed from "
[ 1232s]                 "the main namespace in the future. Use an array with a string "
[ 1232s]                 "or bytes dtype instead.", DeprecationWarning, stacklevel=2)
[ 1232s]             import numpy.char as char
[ 1232s]             return char.chararray
[ 1232s]     
[ 1232s] >       raise AttributeError("module {!r} has no attribute "
[ 1232s]                              "{!r}".format(__name__, attr))
[ 1232s] E       AttributeError: module 'numpy' has no attribute 'alltrue'
[ 1232s] 
[ 1232s] /usr/lib64/python3.10/site-packages/numpy/__init__.py:410: AttributeError

And so on. Please fix before the 4.2.2 release

@radoering
Copy link
Contributor Author

The unit tests have been missed by this PR

Interesting. I assume ruff missed that because numpy is not imported directly in the unit test but via a star import from wx.lib.floatcanvas.Utilities.BBox. Lessons learned: ruff cannot replace a full-text search.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants