From 81cd2a4ee3600a79360fe547189e203360a831dd Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 17 Nov 2025 13:06:52 +0100 Subject: [PATCH 1/5] Implement dpnp.ndarray.__bytes__() method --- dpnp/dpnp_array.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 3e747f00ee4..656f099a0c4 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -185,6 +185,10 @@ def __bool__(self, /): """``True`` if `self` else ``False``.""" return self._array_obj.__bool__() + def __bytes__(self): + r"""Return :math:`\text{bytes(self)}`.""" + return bytes(self.asnumpy()) + # '__class__', # `__class_getitem__`, From 5158cb3abbeed2073a4891a25718f6c65e8f8a89 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 17 Nov 2025 13:07:43 +0100 Subject: [PATCH 2/5] Render documentatin for the new method --- doc/reference/ndarray.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/reference/ndarray.rst b/doc/reference/ndarray.rst index 09d60a307b0..6fc1fb07675 100644 --- a/doc/reference/ndarray.rst +++ b/doc/reference/ndarray.rst @@ -436,6 +436,7 @@ and return the appropriate scalar. :toctree: generated/ :nosignatures: + ndarray.__bytes__ ndarray.__index__ ndarray.__int__ ndarray.__float__ From 6ce842f8cca2e22d7e2a35960770fe032c1c8fbe Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 17 Nov 2025 13:08:08 +0100 Subject: [PATCH 3/5] Enable third party tests --- dpnp/tests/third_party/cupy/core_tests/test_ndarray.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py index 6f79a6a231f..c847b4869fd 100644 --- a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py +++ b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py @@ -653,21 +653,18 @@ def test_size_zero_dim_array_with_axis(self): class TestPythonInterface(unittest.TestCase): - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes(self, xp, dtype): x = testing.shaped_arange((3, 4, 5), xp, dtype) return bytes(x) - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty(self, xp, dtype): x = xp.empty((0,), dtype) return bytes(x) - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty2(self, xp, dtype): @@ -678,7 +675,6 @@ def test_bytes_tobytes_empty2(self, xp, dtype): # if scalar is of an integer dtype including bool_. It's spec is # bytes(int): bytes object of size given by the parameter initialized with # null bytes. - @pytest.mark.skip("__bytes__ is not supported") @testing.for_float_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_scalar_array(self, xp, dtype): From a993fae8e9c13d0a82bc01b743d832bf77ad3045 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 17 Nov 2025 13:13:04 +0100 Subject: [PATCH 4/5] Add PR to the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf88d9266d..848e6ae937e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658) * Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656) * Added implementation of `dpnp.ndarray.__format__` method [#2662](https://github.com/IntelPython/dpnp/pull/2662) +* Added implementation of `dpnp.ndarray.__bytes__` method [#2671](https://github.com/IntelPython/dpnp/pull/2671) ### Changed From 010b0f552d56bebfb6ef4b2ab7ac0c68e4f9d77b Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 17 Nov 2025 13:15:10 +0100 Subject: [PATCH 5/5] Pass dtype as a keyword to empty() in the enabled tests --- dpnp/tests/third_party/cupy/core_tests/test_ndarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py index c847b4869fd..200a29d2926 100644 --- a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py +++ b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py @@ -662,13 +662,13 @@ def test_bytes_tobytes(self, xp, dtype): @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty(self, xp, dtype): - x = xp.empty((0,), dtype) + x = xp.empty((0,), dtype=dtype) return bytes(x) @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty2(self, xp, dtype): - x = xp.empty((3, 0, 4), dtype) + x = xp.empty((3, 0, 4), dtype=dtype) return bytes(x) # The result of bytes(numpy.array(scalar)) is the same as bytes(scalar)