Migrate Python bindings from pybind11 to nanobind #1830
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replaces pybind11 v2.9 with nanobind v2.2.0 for Python bindings to leverage smaller binary sizes, faster compilation, and better Python 3.12+ support.
Changes
Build System
cmake/PythonWrappings.cmaketo fetch nanobind instead of pybind11, usingFetchContent_MakeAvailablewith proper Python package discoverypybind11_add_moduletonanobind_add_moduleinwrap/CMakeLists.txtAPI Migration
dgtal_nanobind_common.hwith nanobind includes and type convertersPYBIND11_MAKE_OPAQUE→NB_MAKE_OPAQUEPYBIND11_MODULE→NB_MODULEpy::withnb::throughout (21 .cpp files, 25 .h files)py::module→nb::module_py::return_value_policy::→nb::rv_policy::py::buffer_protocol→ removed (different API)API Differences (Partially Complete)
Color_py.cppusing static_castRemaining Work
The following nanobind API differences need addressing before compilation succeeds:
def_readwrite→def_rwdef_property_readonly_static→def_prop_ro_staticmake_iteratorsignature changes.cast<>())Example
Before (pybind11):
After (nanobind):
Note: This PR migrates ~80% of the binding code. Additional API-specific changes are needed for successful compilation and testing.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com/usr/lib/apt/methods/https /usr/lib/apt/methods/https -I /home/REDACTED/work/DGtal/DGtal/build/_deps/boost-src/libs/array/include -I /home/REDACTED/work/DGtal/DGtal/build/_deps/boost-src/libs/asio/include -I /home/REDACTED/work/DGtal/DGtal/build/_deps/boost-src/libs/assert/include -I /home/REDACTED/work/DGtal/DGtal/build/_deps/boost-src/libs/assign/include -I /home/REDACTED/work/DGtal/DGtal/build/_deps/boost-src/libs/atomic/include -I inde�� nizer.git --fix-thin e/git tained-and-conneas(dns block)gitlab.com/usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED ps -c gon.git(dns block)/usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED e/git r cmake_echo_color--not --switch= e/git --bold --progress-dir=/rev-list --progress-num=1-n .git r(dns block)Co-authorrev-list p/bin/git e/git-remote-htt1 -S/h�� om/boostorg/icl.--not --check-build-sy--all e/git 0` (dns block)
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.