pybind / pybind/pybind11

[BUG] Callbacks accepting Eigen::Array of custom types

Open
#2,905 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

Issue description

I have a callback method that used to (about two years ago) accept an Eigen::Array of a custom type that could be either integer or double (the independent variable in an optimization problem). Now that code no longer works, and I get errors like:

RuntimeError: NumPy type info missing for struct CEGO::numberish

I've narrowed the problem down to callbacks that take Eigen::Array of non POD types, and tried to add the necessary type definitions, but whatever I do I cannot get it to work like it used to. The library is basically toast. Oddly, the binder instance of the library still works fine, which runs older versions of pybind11/Eigen/python, so the regression has happened since then.

Maybe this should never have worked since Eigen doesn't like heterogeneous types, but it did used to work nonetheless.

Reproducible example code

// My custom double/int type (recommendations are welcomed)
struct numberish {
        enum types { EMPTY, INT, DOUBLE } type;
        numberish() { u.d = std::numeric_limits<double>::max(); type = EMPTY; }
        numberish(const int &value) { u.i = value; type = INT; }
        numberish(const long unsigned int& value) { u.i = value; type = INT; }
        numberish(const double &value) { u.d = value; type = DOUBLE; }
        union id {
            double d;
            int i;
        } u;
        ...
};

// The new DTYPE definitions I tried to add
PYBIND11_NUMPY_DTYPE(CEGO::numberish::id, i, d);
PYBIND11_NUMPY_DTYPE(CEGO::numberish, type, u);

// The exposed method
template <typename T> using EArray = Eigen::Array<T, Eigen::Dynamic, 1, 0, Eigen::Dynamic, 1>;
m.def("indexer", [](std::function<CEGO::numberish(const EArray<CEGO::numberish> &)> &f){ 
        EArray<CEGO::numberish> a(10); a = 3.9; return f(a); 
    });

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 at the m.def("indexer") callback entry point and inspect the Eigen::Array and PYBIND11_NUMPY_DTYPE usage in the reproducer. Compare the current behavior with the older pybind11, Eigen, and Python versions mentioned; done means the callback accepts an Eigen::Array of CEGO::numberish without the NumPy type-info error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.