diff --git a/dpnp/dpnp_iface_indexing.py b/dpnp/dpnp_iface_indexing.py index 21ec32f2400..b060a8e6a62 100644 --- a/dpnp/dpnp_iface_indexing.py +++ b/dpnp/dpnp_iface_indexing.py @@ -69,6 +69,7 @@ "fill_diagonal", "flatnonzero", "indices", + "iterable", "ix_", "mask_indices", "ndindex", @@ -1033,6 +1034,47 @@ def indices( return res +def iterable(y): + """ + Check whether or not an object can be iterated over. + + For full documentation refer to :obj:`numpy.iterable`. + + Parameters + ---------- + y : object + Input object. + + Returns + ------- + out : bool + Return ``True`` if the object has an iterator method or is a sequence + and ``False`` otherwise. + + Examples + -------- + >>> import dpnp as np + >>> np.iterable([1, 2, 3]) + True + >>> np.iterable(2) + False + + In most cases, the results of ``np.iterable(obj)`` are consistent with + ``isinstance(obj, collections.abc.Iterable)``. One notable exception is + the treatment of 0-dimensional arrays: + + >>> from collections.abc import Iterable + >>> a = np.array(1.0) # 0-dimensional array + >>> isinstance(a, Iterable) + True + >>> np.iterable(a) + False + + """ + + return numpy.iterable(y) + + def ix_(*args): """Construct an open mesh from multiple sequences. diff --git a/dpnp/tests/test_indexing.py b/dpnp/tests/test_indexing.py index 2bff01cf1b5..61b91015461 100644 --- a/dpnp/tests/test_indexing.py +++ b/dpnp/tests/test_indexing.py @@ -337,6 +337,14 @@ def test_ix_error(self, xp, shape): assert_raises(ValueError, xp.ix_, xp.ones(shape)) +class TestIterable: + @pytest.mark.parametrize("data", [[1.0], [2, 3]]) + def test_basic(self, data): + a = numpy.array(data) + ia = dpnp.array(a) + assert dpnp.iterable(ia) == numpy.iterable(a) + + @pytest.mark.parametrize( "shape", [[1, 2, 3], [(1, 2, 3)], [(3,)], [3], [], [()], [0]] )