Skip to content

Commit 74a767d

Browse files
bemichelBertrand MICHELYannickJadoul
authored
Dtype kind vs char (#2864)
* [dtype]: add type() method to access type attribute of PyArray_Descr (eq. to dtype.char in Python) * [dtype] change type() name method to char_() to be compliant with Python numpy interface * [dtype] fix by pre-commit * [dtype] Change comments and solutions format for test * Clarify documentation and move note about dtype.char vs PyArray_Descr::type to a plain, non-doxygen comment * Fix and extend tests * Fix the supposedly fixed tests * Fix the fixed tests again Co-authored-by: Bertrand MICHEL <[email protected]> Co-authored-by: Yannick Jadoul <[email protected]>
1 parent c0fbb02 commit 74a767d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

include/pybind11/numpy.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,21 @@ class dtype : public object {
507507
return detail::array_descriptor_proxy(m_ptr)->names != nullptr;
508508
}
509509

510-
/// Single-character type code.
510+
/// Single-character code for dtype's kind.
511+
/// For example, floating point types are 'f' and integral types are 'i'.
511512
char kind() const {
512513
return detail::array_descriptor_proxy(m_ptr)->kind;
513514
}
514515

516+
/// Single-character for dtype's type.
517+
/// For example, ``float`` is 'f', ``double`` 'd', ``int`` 'i', and ``long`` 'd'.
518+
char char_() const {
519+
// Note: The signature, `dtype::char_` follows the naming of NumPy's
520+
// public Python API (i.e., ``dtype.char``), rather than its internal
521+
// C API (``PyArray_Descr::type``).
522+
return detail::array_descriptor_proxy(m_ptr)->type;
523+
}
524+
515525
private:
516526
static object _dtype_from_pep3118() {
517527
static PyObject *obj = module_::import("numpy.core._internal")

tests/test_numpy_dtypes.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ TEST_SUBMODULE(numpy_dtypes, m) {
358358
});
359359

360360
// test_dtype
361+
std::vector<const char *> dtype_names{
362+
"byte", "short", "intc", "int_", "longlong",
363+
"ubyte", "ushort", "uintc", "uint", "ulonglong",
364+
"half", "single", "double", "longdouble",
365+
"csingle", "cdouble", "clongdouble",
366+
"bool_", "datetime64", "timedelta64", "object_"
367+
};
368+
361369
m.def("print_dtypes", []() {
362370
py::list l;
363371
for (const py::handle &d : {
@@ -376,6 +384,18 @@ TEST_SUBMODULE(numpy_dtypes, m) {
376384
return l;
377385
});
378386
m.def("test_dtype_ctors", &test_dtype_ctors);
387+
m.def("test_dtype_kind", [dtype_names]() {
388+
py::list list;
389+
for (auto& dt_name : dtype_names)
390+
list.append(py::dtype(dt_name).kind());
391+
return list;
392+
});
393+
m.def("test_dtype_char_", [dtype_names]() {
394+
py::list list;
395+
for (auto& dt_name : dtype_names)
396+
list.append(py::dtype(dt_name).char_());
397+
return list;
398+
});
379399
m.def("test_dtype_methods", []() {
380400
py::list list;
381401
auto dt1 = py::dtype::of<int32_t>();

tests/test_numpy_dtypes.py

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def test_dtype(simple_dtype):
169169
np.zeros(1, m.trailing_padding_dtype())
170170
)
171171

172+
assert m.test_dtype_kind() == list("iiiiiuuuuuffffcccbMmO")
173+
assert m.test_dtype_char_() == list("bhilqBHILQefdgFDG?MmO")
174+
172175

173176
def test_recarray(simple_dtype, packed_dtype):
174177
elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)]

0 commit comments

Comments
 (0)