Skip to content

Commit b2d9165

Browse files
authored
Merge pull request numpy#27112 from seberg/issue-27106
BUG: Fix repr for integer scalar subclasses
2 parents bf822ea + f636dfd commit b2d9165

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

numpy/_core/src/multiarray/scalartypes.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ genint_type_repr(PyObject *self)
562562
int num = _typenum_fromtypeobj((PyObject *)Py_TYPE(self), 0);
563563

564564
PyObject *repr;
565-
if (num == 0) {
565+
if (num == NPY_NOTYPE) {
566566
/* Not a builtin scalar (presumably), just use the name */
567-
repr = PyUnicode_FromFormat("%S(%S)", Py_TYPE(self)->tp_name, value_string);
567+
repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name, value_string);
568568
Py_DECREF(value_string);
569569
return repr;
570570
}

numpy/_core/tests/test_scalarinherit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def test_gh_15395(self):
5454
with pytest.raises(TypeError):
5555
B1(1.0, 2.0)
5656

57+
def test_int_repr(self):
58+
# Test that integer repr works correctly for subclasses (gh-27106)
59+
class my_int16(np.int16):
60+
pass
61+
62+
s = repr(my_int16(3))
63+
assert s == "my_int16(3)"
5764

5865
class TestCharacter:
5966
def test_char_radd(self):

0 commit comments

Comments
 (0)