Skip to content

Commit b9440c1

Browse files
Merge branch 'master' into refactor_submodule_exports
2 parents 2c60677 + 789d3eb commit b9440c1

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
2323
* Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635)
2424
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
2525
* Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656)
26+
* Added implementation of `dpnp.ndarray.__format__` method [#2662](https://github.com/IntelPython/dpnp/pull/2662)
2627

2728
### Changed
2829

doc/reference/ndarray.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,4 @@ String representations:
449449

450450
ndarray.__str__
451451
ndarray.__repr__
452+
ndarray.__format__

dpnp/dpnp_array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ def __floordiv__(self, other, /):
302302
r"""Return :math:`\text{self // value}`."""
303303
return dpnp.floor_divide(self, other)
304304

305-
# '__format__',
305+
def __format__(self, format_spec):
306+
r"""Return :math:`\text{format(self, format_spec)}`."""
307+
return format(self.asnumpy(), format_spec)
306308

307309
def __ge__(self, other, /):
308310
r"""Return :math:`\text{self >= value}`."""

dpnp/tests/test_ndarray.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ def test_strides(self):
108108
assert xp.full_like(a, fill_value=6) not in a
109109

110110

111+
class TestFormat:
112+
def test_basic(self):
113+
a = numpy.array(3.14159)
114+
ia = dpnp.array(a)
115+
116+
spec = ".3f"
117+
assert_equal(format(ia, spec), format(a, spec))
118+
119+
@pytest.mark.parametrize("xp", [dpnp, numpy])
120+
def test_1d(self, xp):
121+
a = xp.array([3.14159])
122+
with pytest.raises(TypeError, match="unsupported format string"):
123+
_ = format(a, ".2f")
124+
125+
111126
class TestToBytes:
112127
@pytest.mark.parametrize("order", ["C", "F", "K", "A", None])
113128
def test_roundtrip_binary_str(self, order):

dpnp/tests/third_party/cupy/core_tests/test_ndarray.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import copy
24
import unittest
35

@@ -649,21 +651,23 @@ def test_size_zero_dim_array_with_axis(self):
649651
xp.size(x, 0)
650652

651653

652-
@pytest.mark.skip("python interface is not supported")
653654
class TestPythonInterface(unittest.TestCase):
654655

656+
@pytest.mark.skip("__bytes__ is not supported")
655657
@testing.for_all_dtypes()
656658
@testing.numpy_cupy_equal()
657659
def test_bytes_tobytes(self, xp, dtype):
658660
x = testing.shaped_arange((3, 4, 5), xp, dtype)
659661
return bytes(x)
660662

663+
@pytest.mark.skip("__bytes__ is not supported")
661664
@testing.for_all_dtypes()
662665
@testing.numpy_cupy_equal()
663666
def test_bytes_tobytes_empty(self, xp, dtype):
664667
x = xp.empty((0,), dtype)
665668
return bytes(x)
666669

670+
@pytest.mark.skip("__bytes__ is not supported")
667671
@testing.for_all_dtypes()
668672
@testing.numpy_cupy_equal()
669673
def test_bytes_tobytes_empty2(self, xp, dtype):
@@ -674,6 +678,7 @@ def test_bytes_tobytes_empty2(self, xp, dtype):
674678
# if scalar is of an integer dtype including bool_. It's spec is
675679
# bytes(int): bytes object of size given by the parameter initialized with
676680
# null bytes.
681+
@pytest.mark.skip("__bytes__ is not supported")
677682
@testing.for_float_dtypes()
678683
@testing.numpy_cupy_equal()
679684
def test_bytes_tobytes_scalar_array(self, xp, dtype):

0 commit comments

Comments
 (0)