Repository: numpy/numpy
File: numpy/_core/tests/test_api.py (Python)

Code before:
    dat = np.array(memoryview(b'1.0'))
    assert_equal(dat, [49, 46, 48])
    assert_(dat.dtype.type is np.uint8)

    # test array interface
    a = np.array(100.0, dtype=np.float64)
    o = type("o", (object,),
             {"__array_interface__": a.__array_interface__})
    assert_equal(np.array(o, dtype=np.float64), a)

    # test array_struct interface
    a = np.array([(1, 4.0, 'Hello'), (2, 6.0, 'World')],
                 dtype=[('f0', int), ('f1', float), ('f2', str)])
    o = type("o", (object,),
             {"__array_struct__": a.__array_struct__})
    # wasn't what I expected... is np.array(o) supposed to equal a ?
    # instead we get a array([...], dtype=">V18")
    assert_equal(bytes(np.array(o).data), bytes(a.data))

    # test __array__
    def custom__array__(self, dtype=None, copy=None):
        return np.array(100.0, dtype=dtype, copy=copy)

    o = type("o", (object,), {"__array__": custom__array__})()
    assert_equal(np.array(o, dtype=np.float64), np.array(100.0, np.float64))
    if HAS_REFCOUNT:
        class MyArray:
            def __init__(self, dtype):
                self.val = np.array(-1, dtype=dtype)

            def __array__(self, dtype=None, copy=None):
                return self.val.__array__(dtype=dtype, copy=copy)

        dt = np.dtype(np.int32)
        old_refcount = sys.getrefcount(dt)
        np.array(MyArray(dt))
        assert_equal(old_refcount, sys.getrefcount(dt))
        np.array(MyArray(dt), dtype=dt)
        assert_equal(old_refcount, sys.getrefcount(dt))
        np.array(MyArray(dt), copy=None)
        assert_equal(old_refcount, sys.getrefcount(dt))
        np.array(MyArray(dt), dtype=dt, copy=None)
        assert_equal(old_refcount, sys.getrefcount(dt))
        dt2 = np.dtype(np.int16)
        old_refcount2 = sys.getrefcount(dt2)
        np.array(MyArray(dt), dtype=dt2)
        assert_equal(old_refcount2, sys.getrefcount(dt2))
        np.array(MyArray(dt), dtype=dt2, copy=None)
        assert_equal(old_refcount2, sys.getrefcount(dt2))

    # test recursion

Review comment (suggestion):
Although other tests in this same file also check HAS_REFCOUNT with `if`, I think it might be better to use a skipif test instead, so the test doesn't pass silently.  See, for example: https://github.com/numpy/numpy/blob/83402f7a43363e8041b8f297fdd4f76a7fa9fea1/numpy/_core/tests/test_arrayprint.py#L548

Code after:
    dat = np.array(memoryview(b'1.0'))
    assert_equal(dat, [49, 46, 48])
    assert_(dat.dtype.type is np.uint8)

    # test array interface
    a = np.array(100.0, dtype=np.float64)
    o = type("o", (object,),
             {"__array_interface__": a.__array_interface__})
    assert_equal(np.array(o, dtype=np.float64), a)

    # test array_struct interface
    a = np.array([(1, 4.0, 'Hello'), (2, 6.0, 'World')],
                 dtype=[('f0', int), ('f1', float), ('f2', str)])
    o = type("o", (object,),
             {"__array_struct__": a.__array_struct__})
    # wasn't what I expected... is np.array(o) supposed to equal a ?
    # instead we get a array([...], dtype=">V18")
    assert_equal(bytes(np.array(o).data), bytes(a.data))

    # test __array__
    def custom__array__(self, dtype=None, copy=None):
        return np.array(100.0, dtype=dtype, copy=copy)

    o = type("o", (object,), {"__array__": custom__array__})()
    assert_equal(np.array(o, dtype=np.float64), np.array(100.0, np.float64))

    # test recursion
    nested = 1.5
    for i in range(ncu.MAXDIMS):
        nested = [nested]

    # no error
    np.array(nested)

    # Exceeds recursion limit
    assert_raises(ValueError, np.array, [nested], dtype=np.float64)

    # Try with lists...
    # float32
    assert_equal(np.array([None] * 10, dtype=np.float32),
                 np.full((10,), np.nan, dtype=np.float32))
    assert_equal(np.array([[None]] * 10, dtype=np.float32),
                 np.full((10, 1), np.nan, dtype=np.float32))
    assert_equal(np.array([[None] * 10], dtype=np.float32),
                 np.full((1, 10), np.nan, dtype=np.float32))
    assert_equal(np.array([[None] * 10] * 10, dtype=np.float32),
                 np.full((10, 10), np.nan, dtype=np.float32))
    # float64
    assert_equal(np.array([None] * 10, dtype=np.float64),
                 np.full((10,), np.nan, dtype=np.float64))
    assert_equal(np.array([[None]] * 10, dtype=np.float64),