Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dpnp.ndarray.__iter__ method #2206

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ def __isub__(self, other):
dpnp.subtract(self, other, out=self)
return self

# '__iter__',
def __iter__(self):
"""Return ``iter(self)``."""
if self.ndim == 0:
raise TypeError("iteration over a 0-d array")
return (self[i] for i in range(self.shape[0]))

def __itruediv__(self, other):
"""Return ``self/=value``."""
Expand Down
44 changes: 44 additions & 0 deletions dpnp/tests/third_party/cupy/core_tests/test_iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest

import numpy
import pytest

import dpnp as cupy
from dpnp.tests.third_party.cupy import testing


@testing.parameterize(
*testing.product(
{"shape": [(3,), (2, 3, 4), (0,), (0, 2), (3, 0)]},
)
)
class TestIter(unittest.TestCase):

@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_list(self, xp, dtype):
x = testing.shaped_arange(self.shape, xp, dtype)
return list(x)

@testing.for_all_dtypes()
@testing.numpy_cupy_equal()
def test_len(self, xp, dtype):
x = testing.shaped_arange(self.shape, xp, dtype)
return len(x)


class TestIterInvalid(unittest.TestCase):

@testing.for_all_dtypes()
def test_iter(self, dtype):
for xp in (numpy, cupy):
x = testing.shaped_arange((), xp, dtype)
with pytest.raises(TypeError):
iter(x)

@testing.for_all_dtypes()
def test_len(self, dtype):
for xp in (numpy, cupy):
x = testing.shaped_arange((), xp, dtype)
with pytest.raises(TypeError):
len(x)
Loading