-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Declare-then-bind for the Python wrapper for better type generation #1824
Comments
Another problem is cross-module dependency. This is due to circular dependency in GTSAM, for example, for This is slightly harder which basically goes like this http://doxygen.lsst.codes/stack/doxygen/xlink_master_2020_06_24_18.09.15/classlsst_1_1utils_1_1python_1_1_wrapper_collection.html Typical usage: // _mypackage.cc
void wrapClassA(WrapperCollection & wrappers);
void wrapClassB(WrapperCollection & wrappers);
PYBIND11_MODULE(_mypackage, module) {
WrapperCollection wrappers(module, "mypackage");
wrapClassA(wrappers);
wrapClassB(wrappers);
wrappers.finish();
}
// _ClassA.cc
void wrapClassA(WrapperCollection & wrappers) {
wrappers.wrapType(
py::class_<ClassA>(wrappers.module, "ClassA"),
[](auto & mod, auto & cls) {
cls.def("methodOnClassA", &methodOnClassA);
}
);
}
// _ClassB.cc
void wrapClassB(WrapperCollection & wrappers) {
wrappers.wrapType(
py::class_<ClassB>(wrappers.module, "ClassB"),
[](auto & mod, auto & cls) {
cls.def("methodOnClassB", &methodOnClassB);
mod.def("freeFunction", &freeFunction);
}
);
} which literally defers all the lambdas to execute only after all the types are declared. |
Note LSST is GPLv3, so we need to write our own version of the approach, but I think it's not too hard to do. |
I've been getting this a lot. Would it make sense to switch back to |
Currently the Python type generation has a problem: many classes are not registered to pybind11 when they got used as function parameters, resulting in errors like:
This is not an issue for
pybind11-stubgen
ormypy
's stub-gen. This is actually caused by this: https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-c-types-in-docstringsThe solution is "declare-then-bind", which works by first only do the declaration and then after all declarations start binding the functions.
So instead of this:
we should do:
Opening this here as a future TODO.
The text was updated successfully, but these errors were encountered: