summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-11-04 15:07:42 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2021-11-04 15:10:10 -0500
commit6f6802b685b254a0c244ec24244b0a61f4a3c91c (patch)
treea2a5846bf27c23b728ae7516629d8ca91961d2d2 /numpy
parent3cb20a03f5337fe5f038f1593508458849612ba2 (diff)
downloadnumpy-6f6802b685b254a0c244ec24244b0a61f4a3c91c.tar.gz
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.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/experimental_public_dtype_api.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/experimental_public_dtype_api.c b/numpy/core/src/multiarray/experimental_public_dtype_api.c
index ef5030471..4b9c7199b 100644
--- a/numpy/core/src/multiarray/experimental_public_dtype_api.c
+++ b/numpy/core/src/multiarray/experimental_public_dtype_api.c
@@ -131,6 +131,14 @@ PyArrayInitDTypeMeta_FromSpec(
return -1;
}
+ if (((PyTypeObject *)DType)->tp_repr == PyArrayDescr_Type.tp_repr
+ || ((PyTypeObject *)DType)->tp_str == PyArrayDescr_Type.tp_str) {
+ PyErr_SetString(PyExc_TypeError,
+ "A custom DType must implement `__repr__` and `__str__` since "
+ "the default inherited version (currently) fails.");
+ return -1;
+ }
+
if (spec->typeobj == NULL || !PyType_Check(spec->typeobj)) {
PyErr_SetString(PyExc_TypeError,
"Not giving a type object is currently not supported, but "