Skip to content

Commit 223a88a

Browse files
authored
BUG: For new-style wrapped legacy dtypes use old array printing path (numpy#31562)
1 parent 117e338 commit 223a88a

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

numpy/_core/arrayprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ def dtype_short_repr(dtype):
15761576
>>> dt = np.int64([1, 2]).dtype
15771577
>>> assert eval(dtype_short_repr(dt)) == dt
15781578
"""
1579-
if type(dtype).__repr__ != np.dtype.__repr__:
1579+
if not type(dtype)._legacy:
15801580
# TODO: Custom repr for user DTypes, logic should likely move.
15811581
return repr(dtype)
15821582
if dtype.names is not None:

numpy/_core/src/umath/_rational_tests.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,18 @@ static PyTypeObject PyRational2_Type = {
945945
/* the rest is inherited from PyRational_Type via tp_base */
946946
};
947947

948+
949+
static PyObject *
950+
rational2_repr(PyObject *self) {
951+
// Just forward, but old versions of NumPy require a repr
952+
// although for "legacy" dtypes the default one works.
953+
return PyArrayDescr_Type.tp_repr(self);
954+
}
955+
948956
static PyArray_DTypeMeta NPY_Rational2DType = {{{
949957
PyVarObject_HEAD_INIT(NULL, 0)
950958
.tp_name = "numpy._core._rational_tests.Rational2DType",
959+
.tp_repr = (reprfunc)rational2_repr,
951960
}}};
952961

953962
/*

numpy/_core/tests/test_arrayprint.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from hypothesis.extra import numpy as hynp
88

99
import numpy as np
10+
from numpy._core._rational_tests import rational, rational2
1011
from numpy._core.arrayprint import _typelessdata
1112
from numpy._utils import _pep440
1213
from numpy.testing import (
@@ -1351,3 +1352,12 @@ def test_user_defined_floating_dtype_printing_does_not_corrupt_precision():
13511352
res = np.array(str(arr).strip("[] "), dtype=QuadPrecDType())
13521353
# Check that the string representation round-trips correctly.
13531354
assert_array_equal(res, arr)
1355+
1356+
1357+
@pytest.mark.parametrize("sctype", [np.int8, np.float32, rational, rational2])
1358+
def test_array_dtype_short_repr(sctype):
1359+
# Mainly test that rational/rational2 (both legacy dtypes) use short repr
1360+
# which in the end should just be the name for these (not default dtypes).
1361+
arr = np.zeros(1, dtype=sctype)
1362+
res = repr(arr)
1363+
assert f"dtype={sctype.__name__}" in res

0 commit comments

Comments
 (0)