ijl / orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy
Apache License 2.0
6.23k stars 215 forks source link

Test failures with numpy==2.0.0rc1 #480

Closed mgorny closed 6 months ago

mgorny commented 6 months ago

When running the test suite against NumPy 2.0.0rc1, I'm getting the following test failures:

$ python -m pytest -q
............................................................................................................................... [ 10%]
......................................................................................s........................................ [ 20%]
............................................................................................................................... [ 30%]
............................................................................................................................... [ 41%]
.......................................................................................s....................................... [ 51%]
......FFF.......................................................s.............................................................. [ 61%]
............................................................................................................................... [ 71%]
............................................................................................................................... [ 82%]
............................................................................................................................... [ 92%]
s...........................................................................................                                    [100%]
============================================================== FAILURES ===============================================================
_________________________________________________ TestNumpy.test_numpy_array_f16_edge _________________________________________________

self = <test.test_numpy.TestNumpy object at 0x7fa670f93010>

    def test_numpy_array_f16_edge(self):
        assert (
            orjson.dumps(
                numpy.array(
                    [
                        numpy.inf,
                        numpy.NINF,
                        numpy.nan,
                        numpy.NZERO,
                        numpy.PZERO,
                        numpy.pi,
                    ],
                    numpy.float16,
                ),
                option=orjson.OPT_SERIALIZE_NUMPY,
            )
            == b"[null,null,null,-0.0,0.0,3.140625]"
>       )

test/test_numpy.py:169: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

attr = 'NINF'

    def __getattr__(attr):
        # Warn for expired attributes
        import warnings

        if attr == "linalg":
            import numpy.linalg as linalg
            return linalg
        elif attr == "fft":
            import numpy.fft as fft
            return fft
        elif attr == "dtypes":
            import numpy.dtypes as dtypes
            return dtypes
        elif attr == "random":
            import numpy.random as random
            return random
        elif attr == "polynomial":
            import numpy.polynomial as polynomial
            return polynomial
        elif attr == "ma":
            import numpy.ma as ma
            return ma
        elif attr == "ctypeslib":
            import numpy.ctypeslib as ctypeslib
            return ctypeslib
        elif attr == "exceptions":
            import numpy.exceptions as exceptions
            return exceptions
        elif attr == "testing":
            import numpy.testing as testing
            return testing
        elif attr == "matlib":
            import numpy.matlib as matlib
            return matlib
        elif attr == "f2py":
            import numpy.f2py as f2py
            return f2py
        elif attr == "typing":
            import numpy.typing as typing
            return typing
        elif attr == "rec":
            import numpy.rec as rec
            return rec
        elif attr == "char":
            import numpy.char as char
            return char
        elif attr == "array_api":
            raise AttributeError("`numpy.array_api` is not available from "
                                 "numpy 2.0 onwards")
        elif attr == "core":
            import numpy.core as core
            return core
        elif attr == "strings":
            import numpy.strings as strings
            return strings
        elif attr == "distutils":
            if 'distutils' in __numpy_submodules__:
                import numpy.distutils as distutils
                return distutils
            else:
                raise AttributeError("`numpy.distutils` is not available from "
                                     "Python 3.12 onwards")

        if attr in __future_scalars__:
            # And future warnings for those that will change, but also give
            # the AttributeError
            warnings.warn(
                f"In the future `np.{attr}` will be defined as the "
                "corresponding NumPy scalar.", FutureWarning, stacklevel=2)

        if attr in __former_attrs__:
            raise AttributeError(__former_attrs__[attr])

        if attr in __expired_attributes__:
>           raise AttributeError(
                f"`np.{attr}` was removed in the NumPy 2.0 release. "
                f"{__expired_attributes__[attr]}"
            )
E           AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.

.venv/lib/python3.11/site-packages/numpy/__init__.py:397: AttributeError
_________________________________________________ TestNumpy.test_numpy_array_f32_edge _________________________________________________

self = <test.test_numpy.TestNumpy object at 0x7fa670f93610>

    def test_numpy_array_f32_edge(self):
        assert (
            orjson.dumps(
                numpy.array(
                    [
                        numpy.inf,
                        numpy.NINF,
                        numpy.nan,
                        numpy.NZERO,
                        numpy.PZERO,
                        numpy.pi,
                    ],
                    numpy.float32,
                ),
                option=orjson.OPT_SERIALIZE_NUMPY,
            )
            == b"[null,null,null,-0.0,0.0,3.1415927]"
>       )

test/test_numpy.py:188: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

attr = 'NINF'

    def __getattr__(attr):
        # Warn for expired attributes
        import warnings

        if attr == "linalg":
            import numpy.linalg as linalg
            return linalg
        elif attr == "fft":
            import numpy.fft as fft
            return fft
        elif attr == "dtypes":
            import numpy.dtypes as dtypes
            return dtypes
        elif attr == "random":
            import numpy.random as random
            return random
        elif attr == "polynomial":
            import numpy.polynomial as polynomial
            return polynomial
        elif attr == "ma":
            import numpy.ma as ma
            return ma
        elif attr == "ctypeslib":
            import numpy.ctypeslib as ctypeslib
            return ctypeslib
        elif attr == "exceptions":
            import numpy.exceptions as exceptions
            return exceptions
        elif attr == "testing":
            import numpy.testing as testing
            return testing
        elif attr == "matlib":
            import numpy.matlib as matlib
            return matlib
        elif attr == "f2py":
            import numpy.f2py as f2py
            return f2py
        elif attr == "typing":
            import numpy.typing as typing
            return typing
        elif attr == "rec":
            import numpy.rec as rec
            return rec
        elif attr == "char":
            import numpy.char as char
            return char
        elif attr == "array_api":
            raise AttributeError("`numpy.array_api` is not available from "
                                 "numpy 2.0 onwards")
        elif attr == "core":
            import numpy.core as core
            return core
        elif attr == "strings":
            import numpy.strings as strings
            return strings
        elif attr == "distutils":
            if 'distutils' in __numpy_submodules__:
                import numpy.distutils as distutils
                return distutils
            else:
                raise AttributeError("`numpy.distutils` is not available from "
                                     "Python 3.12 onwards")

        if attr in __future_scalars__:
            # And future warnings for those that will change, but also give
            # the AttributeError
            warnings.warn(
                f"In the future `np.{attr}` will be defined as the "
                "corresponding NumPy scalar.", FutureWarning, stacklevel=2)

        if attr in __former_attrs__:
            raise AttributeError(__former_attrs__[attr])

        if attr in __expired_attributes__:
>           raise AttributeError(
                f"`np.{attr}` was removed in the NumPy 2.0 release. "
                f"{__expired_attributes__[attr]}"
            )
E           AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.

.venv/lib/python3.11/site-packages/numpy/__init__.py:397: AttributeError
_________________________________________________ TestNumpy.test_numpy_array_f64_edge _________________________________________________

self = <test.test_numpy.TestNumpy object at 0x7fa670f93c10>

    def test_numpy_array_f64_edge(self):
        assert (
            orjson.dumps(
                numpy.array(
                    [
                        numpy.inf,
                        numpy.NINF,
                        numpy.nan,
                        numpy.NZERO,
                        numpy.PZERO,
                        numpy.pi,
                    ],
                    numpy.float64,
                ),
                option=orjson.OPT_SERIALIZE_NUMPY,
            )
            == b"[null,null,null,-0.0,0.0,3.141592653589793]"
>       )

test/test_numpy.py:207: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

attr = 'NINF'

    def __getattr__(attr):
        # Warn for expired attributes
        import warnings

        if attr == "linalg":
            import numpy.linalg as linalg
            return linalg
        elif attr == "fft":
            import numpy.fft as fft
            return fft
        elif attr == "dtypes":
            import numpy.dtypes as dtypes
            return dtypes
        elif attr == "random":
            import numpy.random as random
            return random
        elif attr == "polynomial":
            import numpy.polynomial as polynomial
            return polynomial
        elif attr == "ma":
            import numpy.ma as ma
            return ma
        elif attr == "ctypeslib":
            import numpy.ctypeslib as ctypeslib
            return ctypeslib
        elif attr == "exceptions":
            import numpy.exceptions as exceptions
            return exceptions
        elif attr == "testing":
            import numpy.testing as testing
            return testing
        elif attr == "matlib":
            import numpy.matlib as matlib
            return matlib
        elif attr == "f2py":
            import numpy.f2py as f2py
            return f2py
        elif attr == "typing":
            import numpy.typing as typing
            return typing
        elif attr == "rec":
            import numpy.rec as rec
            return rec
        elif attr == "char":
            import numpy.char as char
            return char
        elif attr == "array_api":
            raise AttributeError("`numpy.array_api` is not available from "
                                 "numpy 2.0 onwards")
        elif attr == "core":
            import numpy.core as core
            return core
        elif attr == "strings":
            import numpy.strings as strings
            return strings
        elif attr == "distutils":
            if 'distutils' in __numpy_submodules__:
                import numpy.distutils as distutils
                return distutils
            else:
                raise AttributeError("`numpy.distutils` is not available from "
                                     "Python 3.12 onwards")

        if attr in __future_scalars__:
            # And future warnings for those that will change, but also give
            # the AttributeError
            warnings.warn(
                f"In the future `np.{attr}` will be defined as the "
                "corresponding NumPy scalar.", FutureWarning, stacklevel=2)

        if attr in __former_attrs__:
            raise AttributeError(__former_attrs__[attr])

        if attr in __expired_attributes__:
>           raise AttributeError(
                f"`np.{attr}` was removed in the NumPy 2.0 release. "
                f"{__expired_attributes__[attr]}"
            )
E           AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.

.venv/lib/python3.11/site-packages/numpy/__init__.py:397: AttributeError
======================================================= short test summary info =======================================================
FAILED test/test_numpy.py::TestNumpy::test_numpy_array_f16_edge - AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.
FAILED test/test_numpy.py::TestNumpy::test_numpy_array_f32_edge - AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.
FAILED test/test_numpy.py::TestNumpy::test_numpy_array_f64_edge - AttributeError: `np.NINF` was removed in the NumPy 2.0 release. Use `-np.inf` instead.
3 failed, 1228 passed, 4 skipped in 9.47s
mgorny commented 6 months ago

Same goes for NZERO and PZERO.

I can make a pull request, if you'd like. Just let me know if you prefer that I've replaced these constants with the "suggested replacements", or make them conditional to NumPy version (and possibly add "suggested replacements" to the test as well).

ijl commented 6 months ago

It's compatible in 3.10.3. Thanks for the report.

mgorny commented 6 months ago

Thanks. I can confirm that the tests pass for me now!