Skip to content

Commit 61505e4

Browse files
Disallow scalar conversion for non-0D arrays
1 parent d99fa65 commit 61505e4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

dpnp/dpnp_array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def __bytes__(self):
194194

195195
def __complex__(self, /):
196196
"""Convert a zero-dimensional array to a Python complex object."""
197+
self._check_scalar_convertible()
197198
return self._array_obj.__complex__()
198199

199200
def __contains__(self, value, /):
@@ -300,6 +301,7 @@ def __eq__(self, other, /):
300301

301302
def __float__(self, /):
302303
"""Convert a zero-dimensional array to a Python float object."""
304+
self._check_scalar_convertible()
303305
return self._array_obj.__float__()
304306

305307
def __floordiv__(self, other, /):
@@ -391,6 +393,7 @@ def __index__(self, /):
391393

392394
def __int__(self, /):
393395
"""Convert a zero-dimensional array to a Python int object."""
396+
self._check_scalar_convertible()
394397
return self._array_obj.__int__()
395398

396399
def __invert__(self, /):
@@ -608,6 +611,14 @@ def __xor__(self, other, /):
608611
r"""Return :math:`\text{self ^ value}`."""
609612
return dpnp.bitwise_xor(self, other)
610613

614+
def _check_scalar_convertible(self):
615+
"""Raise if array cannot be converted to a Python scalar."""
616+
if self.ndim != 0:
617+
raise TypeError(
618+
"Only 0-dimensional dpnp.ndarray can be converted "
619+
"to a Python scalar"
620+
)
621+
611622
@staticmethod
612623
def _create_from_usm_ndarray(usm_ary: dpt.usm_ndarray):
613624
"""

0 commit comments

Comments
 (0)