pybind / pybind/pybind11

Unable to do the difference between kind() and type() for dtype in numpy.h

Open
#2,860 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Hi,
We are using pybind11 to give access to Python/C++ arrays in on a numerical project. For this, we are using the pybind11:array_t.

But, we would like to have access to type attribute in the PyArray_Descr in API C or the dtype.char on Python interface, but only the kind() method is available in the class pybin11::dtype in numpy.h.

The kind method gives only the "general" kind of the dtype, i.e.

    auto float32 = py::dtype::of<float>();
    std::cout << "float32.kind() = " << float32.kind() << std::endl; /// return 'f'
    auto float64 = py::dtype::of<double>();
    std::cout << "float64.kind() = " << float64.kind() << std::endl; /// return 'f'

So it is not possible to distinguish float from double, or int from long int.

Only by adding a new method in the pybind11::dtype::type() for exemple which returns the type attribute from PyArray_Descr, like the following

class dtype : public object {
  ....
    /// Single-character for dtype's kind (ex: float and double are 'f' or int and long int are 'i')
    char kind() const {
        return detail::array_descriptor_proxy(m_ptr)->kind;
    }

    /// Single-character for dtype's type (ex: float is 'f' and double 'd')
    char type() const {
        return detail::array_descriptor_proxy(m_ptr)->type;
    }
private:
  ....
};

It will be now possible to distinguish the effective type

    auto float32 = py::dtype::of<float>();
    std::cout << "float32.kind() = " << float32.kind() << std::endl; /// return 'f'
    std::cout << "float32.type() = " << float32.type() << std::endl; /// return 'f'
    auto float64 = py::dtype::of<double>();
    std::cout << "float64.kind() = " << float64.kind() << std::endl; /// return 'f'
    std::cout << "float64.type() = " << float64.type() << std::endl; /// return 'd'

Sorry, I am not sure if it is the good way to asking to add this little piece of code and/or if it is just possible ?
Should I create a merge request instead ?

Best regards,
Bertrand M.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in numpy.h at py::dtype::kind() and detail::array_descriptor_proxy, then review the surrounding dtype API and existing tests. Confirm how NumPy exposes kind and type for representative dtypes such as float32 and float64. Done means the dtype interface distinguishes those types and includes coverage for the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.