[QUESTION] inherit from pybind::array_t<T>
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
I pass many numpy arrays to C++ code using pybind11, in particular pybind11::array_t
I want to augment the interface of pybind11::array_t through inheriting from pybind11::array_t.
currently I need a lot of code to check the dimensions of the arrays and access their data. This I can solve by wrapping the argument py::array_t but it would be more elegant if the function arguments would be directly
E.g. a function that requires a 1D array as input
double sum ( py::array_t x) {
// check that x is 1D
// compute the sum and return it
}
would become simpler like this
double sum ( my_array_t<double,1> x {
// compute the sum and return it
}
my_array_t<T,NDIM> inherits publicly from py::array_t and its constructor would check that the argument is indeed 1D.
template<class T, size_t NDIM>
class my_array_t : public py::array_t<T>
{
public:
ArrayInfo(py::handle h, py::object::borrowed_t)
: py::array_t<T>(h, py::object::borrowed_t{})
{
// check that the argument is indeed 1D
}
};
When I try to compile this, however, I get the following error
/Library/Developer/CommandLineTools/usr/bin/c++ -Dai_EXPORTS -isystem /Users/etijskens/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pybind11/include -isystem /Users/etijskens/.pyenv/versions/3.8.5/include/python3.8 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -fPIC -fvisibility=hidden -std=gnu++11 -MD -MT CMakeFiles/ai.dir/ai.cpp.o -MF CMakeFiles/ai.dir/ai.cpp.o.d -o CMakeFiles/ai.dir/ai.cpp.o -c /Users/etijskens/software/dev/workspace/arrayinfo/arrayinfo/cpp_ai/ai.cpp
In file included from /Users/etijskens/software/dev/workspace/arrayinfo/arrayinfo/cpp_ai/ai.cpp:8:
/Users/etijskens/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:171:21: error: call to implicitly-deleted default constructor of 'cast_in' (aka 'argument_loader<my_array_t<double, 1>>')
cast_in args_converter;
^
/Users/etijskens/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:70:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<void (*&)(my_array_t<double, 1>), void, my_array_t<double, 1>, pybind11::name, pybind11::scope, pybind11::sibling>' requested here
initialize(f, f, extra...);
^
/Users/etijskens/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:962:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, my_array_t<double, 1>, pybind11::name, pybind11::scope, pybind11::sibling>' requested here
cpp_function func(std::forward(f), name(name_), scope(this),
^
/Users/etijskens/software/dev/workspace/arrayinfo/arrayinfo/cpp_ai/ai.cpp:47:6: note: in instantiation of function template specialization 'pybind11::module_::def<void ()(my_array_t<double, 1>)>' requested here
m.def("sum", &sum);
^
/Users/etijskens/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pybind11/include/pybind11/cast.h:2045:38: note: default constructor of 'argument_loader<my_array_t<double, 1>>' is implicitly deleted because field 'argcasters' has no default constructor
std::tuple<make_caster...> argcasters;
^
1 error generated.
make[2]: *** [CMakeFiles/ai.dir/ai.cpp.o] Error 1
make[1]: *** [CMakeFiles/ai.dir/all] Error 2
make: *** [all] Error 2
All help is welcome
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported binding with the sum function and my_array_t<double, 1> example, then inspect pybind11/pybind11.h and pybind11/cast.h, especially argument_loader and the m.def("sum", &sum) entry point. Done means establishing whether this inheritance pattern can be supported and documenting the result or a focused project change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100