Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions doc/reference/ndarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,4 @@ String representations:

ndarray.__str__
ndarray.__repr__
ndarray.__format__
4 changes: 3 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}`."""
Expand Down
15 changes: 15 additions & 0 deletions dpnp/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion dpnp/tests/third_party/cupy/core_tests/test_ndarray.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import copy
import unittest

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Loading