Skip to content

Commit 6f6802b

Browse files
committed
ENH: Check for __repr__ and __str__ implementation
This adds a check whether `__repr__` or `__str__` appear to be inherited. If they are, it currently leads to cryptic errors adding an unnecessary difficulty to creating a custom user DType.
1 parent 3cb20a0 commit 6f6802b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

numpy/core/src/multiarray/experimental_public_dtype_api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ PyArrayInitDTypeMeta_FromSpec(
131131
return -1;
132132
}
133133

134+
if (((PyTypeObject *)DType)->tp_repr == PyArrayDescr_Type.tp_repr
135+
|| ((PyTypeObject *)DType)->tp_str == PyArrayDescr_Type.tp_str) {
136+
PyErr_SetString(PyExc_TypeError,
137+
"A custom DType must implement `__repr__` and `__str__` since "
138+
"the default inherited version (currently) fails.");
139+
return -1;
140+
}
141+
134142
if (spec->typeobj == NULL || !PyType_Check(spec->typeobj)) {
135143
PyErr_SetString(PyExc_TypeError,
136144
"Not giving a type object is currently not supported, but "

0 commit comments

Comments
 (0)