Skip to content

Commit f88d6af

Browse files
committed
Add more tests to cover more use cases
1 parent b457f9d commit f88d6af

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

dpnp/dpnp_array.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,14 +1990,15 @@ def view(self, dtype=None, *, type=None):
19901990
----------
19911991
dtype : {None, str, dtype object}, optional
19921992
The desired data type of the returned view, e.g. :obj:`dpnp.float32`
1993-
or :obj:`dpnp.int16`. Omitting it results in the view having the
1993+
or :obj:`dpnp.int16`. By default, it results in the view having the
19941994
same data type.
19951995
1996+
Default: ``None``.
1997+
19961998
Notes
19971999
-----
1998-
Passing ``None`` for `dtype` is different from omitting the parameter,
1999-
since the former invokes ``dtype(None)`` which is an alias for the
2000-
default floating point data type.
2000+
Passing ``None`` for `dtype` is the same as omitting the parameter,
2001+
opposite to NumPy where they have different meaning.
20012002
20022003
``view(some_dtype)`` or ``view(dtype=some_dtype)`` constructs a view of
20032004
the array's memory with a different data type. This can cause a

dpnp/tests/test_ndarray.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ def test_attributes(self):
7474
assert_equal(self.two.itemsize, self.two.dtype.itemsize)
7575

7676

77+
class TestView:
78+
def test_none_dtype(self):
79+
a = numpy.ones((1, 2, 4), dtype=numpy.int32)
80+
ia = dpnp.array(a)
81+
82+
expected = a.view()
83+
result = ia.view()
84+
assert_allclose(result, expected)
85+
86+
expected = a.view() # numpy returns dtype(None) otherwise
87+
result = ia.view(None)
88+
assert_allclose(result, expected)
89+
90+
@pytest.mark.parametrize("dt", [bool, int, float, complex])
91+
def test_python_types(self, dt):
92+
a = numpy.ones((8, 4), dtype=numpy.complex64)
93+
ia = dpnp.array(a)
94+
95+
expected = a.view(dt)
96+
result = ia.view(dt)
97+
assert_allclose(result, expected)
98+
99+
def test_type_error(self):
100+
x = dpnp.ones(4, dtype="i4")
101+
with pytest.raises(NotImplementedError):
102+
x.view("i2", type=dpnp.ndarray)
103+
104+
77105
@pytest.mark.parametrize(
78106
"arr",
79107
[

0 commit comments

Comments
 (0)