diff --git a/array_api_strict/_array_object.py b/array_api_strict/_array_object.py index 8849ce3..18ed327 100644 --- a/array_api_strict/_array_object.py +++ b/array_api_strict/_array_object.py @@ -647,6 +647,15 @@ def __invert__(self: Array, /) -> Array: res = self._array.__invert__() return self.__class__._new(res) + def __iter__(self: Array, /): + """ + Performs the operation __iter__. + """ + # Manually disable iteration, since __getitem__ raises IndexError on + # things like ones((3, 3))[0], which causes list(ones((3, 3))) to give + # []. + raise TypeError("array iteration is not allowed in array-api-strict") + def __le__(self: Array, other: Union[int, float, Array], /) -> Array: """ Performs the operation __le__. diff --git a/array_api_strict/tests/test_array_object.py b/array_api_strict/tests/test_array_object.py index e061a94..407bff2 100644 --- a/array_api_strict/tests/test_array_object.py +++ b/array_api_strict/tests/test_array_object.py @@ -416,3 +416,7 @@ def test_array_namespace(): pytest.raises(ValueError, lambda: a.__array_namespace__(api_version="2021.11")) pytest.raises(ValueError, lambda: a.__array_namespace__(api_version="2023.12")) + +def test_no_iter(): + pytest.raises(TypeError, lambda: iter(ones(3))) + pytest.raises(TypeError, lambda: iter(ones((3, 3))))