Skip to content

Commit 6655e09

Browse files
authored
Merge 52b8052 into 6cc2348
2 parents 6cc2348 + 52b8052 commit 6655e09

File tree

10 files changed

+40
-9
lines changed

10 files changed

+40
-9
lines changed

dpnp/dpnp_array.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ def __and__(self, other):
185185
"""Return ``self&value``."""
186186
return dpnp.bitwise_and(self, other)
187187

188-
# '__array__',
188+
def __array__(self, dtype=None, /, *, copy=None):
189+
raise TypeError(
190+
"Implicit conversion to a NumPy array is not allowed. "
191+
"Please use `.asnumpy()` to construct a NumPy array explicitly."
192+
)
193+
189194
# '__array_finalize__',
190195
# '__array_function__',
191196
# '__array_interface__',

dpnp/dpnp_utils/dpnp_utils_pad.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ def _as_pairs(x, ndim, as_index=False):
7474
x = round(x)
7575
return ((x, x),) * ndim
7676

77-
x = numpy.array(x)
77+
# explicitly cast input "x" to NumPy array
78+
if dpnp.is_supported_array_type(x):
79+
x = dpnp.asnumpy(x)
80+
else:
81+
x = numpy.array(x)
82+
7883
if as_index:
7984
x = numpy.asarray(numpy.round(x), dtype=numpy.intp)
8085

dpnp/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from . import testing
44

55
numpy.testing.assert_allclose = testing.assert_allclose
6+
numpy.testing.assert_almost_equal = testing.assert_almost_equal
67
numpy.testing.assert_array_equal = testing.assert_array_equal
78
numpy.testing.assert_equal = testing.assert_equal

dpnp/tests/helper.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ def get_all_dtypes(
161161
return dtypes
162162

163163

164+
def get_array(xp, a):
165+
"""
166+
Cast input array `a` to a type supported by `xp` initerface.
167+
168+
Implicit conversion of either DPNP or DPCTL array to a NumPy array is not
169+
allowed. Input array has to be explicitly casted with `asnumpy` function.
170+
171+
"""
172+
173+
if xp is numpy and dpnp.is_supported_array_type(a):
174+
return dpnp.asnumpy(a)
175+
return a
176+
177+
164178
def generate_random_numpy_array(
165179
shape,
166180
dtype=None,

dpnp/tests/test_arraycreation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .helper import (
1818
assert_dtype_allclose,
1919
get_all_dtypes,
20+
get_array,
2021
)
2122
from .third_party.cupy import testing
2223

@@ -768,7 +769,7 @@ def test_space_numpy_dtype(func, start_dtype, stop_dtype):
768769
],
769770
)
770771
def test_linspace_arrays(start, stop):
771-
func = lambda xp: xp.linspace(start, stop, 10)
772+
func = lambda xp: xp.linspace(get_array(xp, start), get_array(xp, stop), 10)
772773
assert func(numpy).shape == func(dpnp).shape
773774

774775

dpnp/tests/testing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .array import (
22
assert_allclose,
3+
assert_almost_equal,
34
assert_array_equal,
45
assert_equal,
56
)

dpnp/tests/testing/array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from dpnp.dpnp_utils import convert_item
3030

3131
assert_allclose_orig = numpy.testing.assert_allclose
32+
assert_almost_equal_orig = numpy.testing.assert_almost_equal
3233
assert_array_equal_orig = numpy.testing.assert_array_equal
3334
assert_equal_orig = numpy.testing.assert_equal
3435

@@ -44,6 +45,10 @@ def assert_allclose(result, expected, *args, **kwargs):
4445
_assert(assert_allclose_orig, result, expected, *args, **kwargs)
4546

4647

48+
def assert_almost_equal(result, expected, *args, **kwargs):
49+
_assert(assert_almost_equal_orig, result, expected, *args, **kwargs)
50+
51+
4752
def assert_array_equal(result, expected, *args, **kwargs):
4853
_assert(assert_array_equal_orig, result, expected, *args, **kwargs)
4954

dpnp/tests/third_party/cupy/core_tests/test_ndarray.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ def test_format(self, xp):
691691
return format(x, ".2f")
692692

693693

694-
@pytest.mark.skip("implicit conversation to numpy does not raise an exception")
695694
class TestNdarrayImplicitConversion(unittest.TestCase):
696695

697696
def test_array(self):

dpnp/tests/third_party/cupy/random_tests/test_permutations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class TestPermutationSoundness(unittest.TestCase):
129129

130130
def setUp(self):
131131
a = cupy.random.permutation(self.num)
132-
self.a = a
132+
self.a = a.asnumpy()
133133

134134
# Test soundness
135135

dpnp/tests/third_party/cupy/random_tests/test_sample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def test_bound_float2(self):
8989
def test_goodness_of_fit(self):
9090
mx = 5
9191
trial = 100
92-
vals = [random.randint(mx) for _ in range(trial)]
92+
vals = [random.randint(mx).asnumpy() for _ in range(trial)]
9393
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
9494
expected = numpy.array([float(trial) / mx] * mx)
9595
assert _hypothesis.chi_square_test(counts, expected)
9696

9797
@_condition.repeat(3, 10)
9898
def test_goodness_of_fit_2(self):
9999
mx = 5
100-
vals = random.randint(mx, size=(5, 20))
100+
vals = random.randint(mx, size=(5, 20)).asnumpy()
101101
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
102102
expected = numpy.array([float(vals.size) / mx] * mx)
103103
assert _hypothesis.chi_square_test(counts, expected)
@@ -191,15 +191,15 @@ def test_bound_2(self):
191191
def test_goodness_of_fit(self):
192192
mx = 5
193193
trial = 100
194-
vals = [random.randint(0, mx) for _ in range(trial)]
194+
vals = [random.randint(0, mx).asnumpy() for _ in range(trial)]
195195
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
196196
expected = numpy.array([float(trial) / mx] * mx)
197197
assert _hypothesis.chi_square_test(counts, expected)
198198

199199
@_condition.repeat(3, 10)
200200
def test_goodness_of_fit_2(self):
201201
mx = 5
202-
vals = random.randint(0, mx, (5, 20))
202+
vals = random.randint(0, mx, (5, 20)).asnumpy()
203203
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
204204
expected = numpy.array([float(vals.size) / mx] * mx)
205205
assert _hypothesis.chi_square_test(counts, expected)

0 commit comments

Comments
 (0)