diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/include/numpy/ndarraytypes.h | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dtypemeta.c | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 17 |
4 files changed, 30 insertions, 15 deletions
diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index db7d7bb30..63c896302 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -1840,9 +1840,9 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size, /* * TODO: Update above comment as necessary. - * Most DTypes will have a singleton default instance, for the flexible - * legacy DTypes (bytes, string, void, datetime) this may be a pointer - * to the *prototype* instance? + * Most DTypes will have a singleton default instance, for the + * parametric legacy DTypes (bytes, string, void, datetime) this + * may be a pointer to the *prototype* instance? */ PyArray_Descr *singleton; /* @@ -1865,7 +1865,7 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size, char type; /* flags describing data type */ char flags; - npy_bool is_flexible; + npy_bool is_parametric; /* whether the DType can be instantiated (i.e. np.dtype cannot) */ npy_bool is_abstract; /* number representing this type */ diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 5bf537816..37373451e 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -3491,5 +3491,5 @@ NPY_NO_EXPORT PyArray_DTypeMeta PyArrayDescr_TypeFull = { .type_num = -1, .kind = '\0', .is_abstract = 1, - .is_flexible = 0, + .is_parametric = 0, }; diff --git a/numpy/core/src/multiarray/dtypemeta.c b/numpy/core/src/multiarray/dtypemeta.c index 23c39606f..943ac299b 100644 --- a/numpy/core/src/multiarray/dtypemeta.c +++ b/numpy/core/src/multiarray/dtypemeta.c @@ -82,11 +82,11 @@ legacy_dtype_default_new(PyArray_DTypeMeta *self, PyObject *args, PyObject *kwargs) { /* TODO: This should allow endianess and possibly metadata */ - if (self->is_flexible) { - /* reject flexible ones since we would need to get unit, etc. info */ + if (self->is_parametric) { + /* reject parametric ones since we would need to get unit, etc. info */ PyErr_Format(PyExc_TypeError, - "Preliminary-API: Flexible legacy DType '%S' can only be " - "instantiated using `np.dtype(...)`", self); + "Preliminary-API: Flexible/Parametric legacy DType '%S' can " + "only be instantiated using `np.dtype(...)`", self); return NULL; } @@ -220,10 +220,10 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr) if (PyTypeNum_ISDATETIME(descr->type_num)) { /* Datetimes are flexible, but were not considered previously */ - dtype_class->is_flexible = NPY_TRUE; + dtype_class->is_parametric = NPY_TRUE; } else if (PyTypeNum_ISFLEXIBLE(descr->type_num)) { - dtype_class->is_flexible = NPY_TRUE; + dtype_class->is_parametric = NPY_TRUE; dtype_class->itemsize = -1; /* itemsize is not fixed */ } @@ -240,11 +240,11 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr) */ static PyMemberDef dtypemeta_members[] = { {"_abstract", - T_BYTE, offsetof(PyArray_DTypeMeta, is_abstract), READONLY, NULL}, + T_BYTE, offsetof(PyArray_DTypeMeta, is_abstract), READONLY, NULL}, {"type", T_OBJECT, offsetof(PyArray_DTypeMeta, scalar_type), READONLY, NULL}, - {"_flexible", - T_BYTE, offsetof(PyArray_DTypeMeta, is_flexible), READONLY, NULL}, + {"_parametric", + T_BYTE, offsetof(PyArray_DTypeMeta, is_parametric), READONLY, NULL}, {NULL, 0, 0, 0, NULL}, }; diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index cbe8322a3..d983a03f8 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -1094,12 +1094,27 @@ class TestFromDTypeAttribute: class TestDTypeClasses: @pytest.mark.parametrize("dtype", list(np.typecodes['All']) + [rational]) - def test_dtypes_are_subclasses(self, dtype): + def test_basic_dtypes_subclass_properties(self, dtype): + # Note: Except for the isinstance and type checks, these attributes + # are considered currently private and may change. dtype = np.dtype(dtype) assert isinstance(dtype, np.dtype) assert type(dtype) is not np.dtype assert type(dtype).__name__ == f"dtype[{dtype.type.__name__}]" assert type(dtype).__module__ == "numpy" + assert not type(dtype)._abstract + + parametric = (np.void, np.str_, np.bytes_, np.datetime64, np.timedelta64) + if dtype.type not in parametric: + assert not type(dtype)._parametric + assert type(dtype)() is dtype + else: + assert type(dtype)._parametric + with assert_raises(TypeError): + type(dtype)() + + def test_dtype_superclass(self): + assert np.dtype._abstract class TestFromCTypes: |