pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.51k stars 2.09k forks source link

[BUG]: test failures with numpy 1.22.1 #3680

Closed mgorny closed 2 years ago

mgorny commented 2 years ago

Required prerequisites

Problem description

Tests run against numpy 1.22.1 have a few new failures. It seems that the str() method on some types changed output.

============================================================== FAILURES ===============================================================
_____________________________________________________________ test_dtype ______________________________________________________________

simple_dtype = dtype({'names': ['bool_', 'uint_', 'float_', 'ldbl_'], 'formats': ['?', '<u4', '<f4', '<f16'], 'offsets': [0, 4, 8, 16], 'itemsize': 32})

    def test_dtype(simple_dtype):
        from sys import byteorder

        e = "<" if byteorder == "little" else ">"

>       assert m.print_dtypes() == [
            simple_dtype_fmt(),
            packed_dtype_fmt(),
            "[('a', {}), ('b', {})]".format(simple_dtype_fmt(), packed_dtype_fmt()),
            partial_dtype_fmt(),
            partial_nested_fmt(),
            "[('a', 'S3'), ('b', 'S3')]",
            (
                "{{'names':['a','b','c','d'], "
                + "'formats':[('S4', (3,)),('"
                + e
                + "i4', (2,)),('u1', (3,)),('"
                + e
                + "f4', (4, 2))], "
                + "'offsets':[0,12,20,24], 'itemsize':56}}"
            ).format(e=e),
            "[('e1', '" + e + "i8'), ('e2', 'u1')]",
            "[('x', 'i1'), ('y', '" + e + "u8')]",
            "[('cflt', '" + e + "c8'), ('cdbl', '" + e + "c16')]",
        ]
E       assert ["{'names': [... 'S3')]", ...] == ["{'names':['... 'S3')]", ...]
E         At index 0 diff: "{'names': ['bool_', 'uint_', 'float_', 'ldbl_'], 'formats': ['?', '<u4', '<f4', '<f16'], 'offsets': [0, 4, 8, 16], 'itemsize': 32}" != "{'names':['bool_','uint_','float_','ldbl_'], 'formats':['?','<u4','<f4','<f16'], 'offsets':[0,4,8,16], 'itemsize':32}"
E         Use -v to get the full diff

../../../../tests/test_numpy_dtypes.py:126: AssertionError
____________________________________________________________ test_recarray ____________________________________________________________

simple_dtype = dtype({'names': ['bool_', 'uint_', 'float_', 'ldbl_'], 'formats': ['?', '<u4', '<f4', '<f16'], 'offsets': [0, 4, 8, 16], 'itemsize': 32})
packed_dtype = dtype([('bool_', '?'), ('uint_', '<u4'), ('float_', '<f4'), ('ldbl_', '<f16')])

    def test_recarray(simple_dtype, packed_dtype):
        elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)]

        for func, dtype in [
            (m.create_rec_simple, simple_dtype),
            (m.create_rec_packed, packed_dtype),
        ]:
            arr = func(0)
            assert arr.dtype == dtype
            assert_equal(arr, [], simple_dtype)
            assert_equal(arr, [], packed_dtype)

            arr = func(3)
            assert arr.dtype == dtype
            assert_equal(arr, elements, simple_dtype)
            assert_equal(arr, elements, packed_dtype)

            # Show what recarray's look like in NumPy.
            assert type(arr[0]) == np.void
            assert type(arr[0].item()) == tuple

            if dtype == simple_dtype:
                assert m.print_rec_simple(arr) == [
                    "s:0,0,0,-0",
                    "s:1,1,1.5,-2.5",
                    "s:0,2,3,-5",
                ]
            else:
                assert m.print_rec_packed(arr) == [
                    "p:0,0,0,-0",
                    "p:1,1,1.5,-2.5",
                    "p:0,2,3,-5",
                ]

        nested_dtype = np.dtype([("a", simple_dtype), ("b", packed_dtype)])

        arr = m.create_rec_nested(0)
        assert arr.dtype == nested_dtype
        assert_equal(arr, [], nested_dtype)

        arr = m.create_rec_nested(3)
        assert arr.dtype == nested_dtype
        assert_equal(
            arr,
            [
                ((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)),
                ((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)),
                ((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5)),
            ],
            nested_dtype,
        )
        assert m.print_rec_nested(arr) == [
            "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
            "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
            "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5",
        ]

        arr = m.create_rec_partial(3)
>       assert str(arr.dtype) == partial_dtype_fmt()
E       assert "{'names': ['...temsize': 48}" == "{'names':['b...itemsize':48}"
E         - {'names':['bool_','uint_','float_','ldbl_'], 'formats':['?','<u4','<f4','<f16'], 'offsets':[0,4,8,32], 'itemsize':48}
E         + {'names': ['bool_', 'uint_', 'float_', 'ldbl_'], 'formats': ['?', '<u4', '<f4', '<f16'], 'offsets': [0, 4, 8, 32], 'itemsize': 48}
E         ?          +         +        +         +                    +     +      +      +                   +   +  +  +                +

../../../../tests/test_numpy_dtypes.py:241: AssertionError
__________________________________________________________ test_array_array ___________________________________________________________

    def test_array_array():
        from sys import byteorder

        e = "<" if byteorder == "little" else ">"

        arr = m.create_array_array(3)
>       assert str(arr.dtype) == (
            "{{'names':['a','b','c','d'], "
            + "'formats':[('S4', (3,)),('"
            + e
            + "i4', (2,)),('u1', (3,)),('{e}f4', (4, 2))], "
            + "'offsets':[0,12,20,24], 'itemsize':56}}"
        ).format(e=e)
E       assert "{'names': ['...temsize': 56}" == "{'names':['a...itemsize':56}"
E         - {'names':['a','b','c','d'], 'formats':[('S4', (3,)),('<i4', (2,)),('u1', (3,)),('<f4', (4, 2))], 'offsets':[0,12,20,24], 'itemsize':56}
E         + {'names': ['a', 'b', 'c', 'd'], 'formats': [('S4', (3,)), ('<i4', (2,)), ('u1', (3,)), ('<f4', (4, 2))], 'offsets': [0, 12, 20, 24], 'itemsize': 56}
E         ?          +     +    +    +                +              +              +             +                            +   +   +   +                +

../../../../tests/test_numpy_dtypes.py:288: AssertionError

Reproducible example code

sed -i -e 's:1.21.3:1.22.1:' tests/requirements.txt
nox -e tests-3.9
henryiii commented 2 years ago

I wonder if there's a good way to ignore whitespace in the compare?

mgorny commented 2 years ago

I'll try experimenting with stripping all whitespace — I don't think we're actually need to relying on any of it being preserved.