From 3cb43ba47378a34580f837c1b0f3cdf402f017fe Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 10 Nov 2025 20:38:01 +0100 Subject: [PATCH 1/5] Implement dpnp.ndarray.__format__() method --- dpnp/dpnp_array.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 365d01a179a..eec3f8d706d 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -306,7 +306,9 @@ def __floordiv__(self, other, /): r"""Return :math:`\text{self // value}`.""" return dpnp.floor_divide(self, other) - # '__format__', + def __format__(self, format_spec): + r"""Return :math:`\text{format(self, format_spec)}`.""" + return format(self.asnumpy(), format_spec) def __ge__(self, other, /): r"""Return :math:`\text{self >= value}`.""" From 19200df45cddefcd97dcab296f9fbbc4201c31e9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 10 Nov 2025 21:06:11 +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 321f2fe95b9..09d60a307b0 100644 --- a/doc/reference/ndarray.rst +++ b/doc/reference/ndarray.rst @@ -449,3 +449,4 @@ String representations: ndarray.__str__ ndarray.__repr__ + ndarray.__format__ From 74829f77ac01806276b67b09bb3873e1395f69ee Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 10 Nov 2025 23:58:37 +0100 Subject: [PATCH 3/5] Enable third party tests --- dpnp/tests/third_party/cupy/core_tests/test_ndarray.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 c7afd483e59..6f79a6a231f 100644 --- a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py +++ b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import copy import unittest @@ -649,21 +651,23 @@ def test_size_zero_dim_array_with_axis(self): xp.size(x, 0) -@pytest.mark.skip("python interface is not supported") 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): @@ -674,6 +678,7 @@ 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 3cf35a1c8d0d5ac94af024b03d07bcd40999beb5 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 11 Nov 2025 09:51:45 +0100 Subject: [PATCH 4/5] Add PR to the chnagelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a994baddb5..179f2d42593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635) * 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) ### Changed From 62bda777e86f9221b9e6c5d297f5dc4620e23ef1 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Tue, 11 Nov 2025 10:03:37 +0100 Subject: [PATCH 5/5] Add more tests --- dpnp/tests/test_ndarray.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 7b334ddbd97..c7e9dc65b99 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -108,6 +108,21 @@ def test_strides(self): assert xp.full_like(a, fill_value=6) not in a +class TestFormat: + def test_basic(self): + a = numpy.array(3.14159) + ia = dpnp.array(a) + + spec = ".3f" + assert_equal(format(ia, spec), format(a, spec)) + + @pytest.mark.parametrize("xp", [dpnp, numpy]) + def test_1d(self, xp): + a = xp.array([3.14159]) + with pytest.raises(TypeError, match="unsupported format string"): + _ = format(a, ".2f") + + class TestToBytes: @pytest.mark.parametrize("order", ["C", "F", "K", "A", None]) def test_roundtrip_binary_str(self, order):