Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 2, 2026

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: Updated cmake/PythonWrappings.cmake to fetch nanobind instead of pybind11, using FetchContent_MakeAvailable with proper Python package discovery
  • Module build: Changed from pybind11_add_module to nanobind_add_module in wrap/CMakeLists.txt

API Migration

  • Headers: Created dgtal_nanobind_common.h with nanobind includes and type converters
    • PYBIND11_MAKE_OPAQUENB_MAKE_OPAQUE
    • Updated STL binding includes to nanobind equivalents
  • Module definitions: PYBIND11_MODULENB_MODULE
  • Namespace: Replaced py:: with nb:: throughout (21 .cpp files, 25 .h files)
  • Types:
    • py::modulenb::module_
    • py::return_value_policy::nb::rv_policy::
    • py::buffer_protocol → removed (different API)

API Differences (Partially Complete)

  • Properties: Fixed overload_cast usage in Color_py.cpp using static_cast
  • Buffer protocol: Temporarily disabled in PointVector, ImageContainer, and KhalimskyPreSpaceND - requires reimplementation with nanobind's ndarray API

Remaining Work

The following nanobind API differences need addressing before compilation succeeds:

  • def_readwritedef_rw
  • def_property_readonly_staticdef_prop_ro_static
  • make_iterator signature changes
  • Pickle support (different API)
  • Type casting methods (.cast<>())

Example

Before (pybind11):

PYBIND11_MODULE(_dgtal, m) {
    py::class_<Color>(m, "Color", py::buffer_protocol())
        .def_property("red",
            py::detail::overload_cast_impl<>()(&Color::red, py::const_),
            py::detail::overload_cast_impl<const unsigned char>()(&Color::red));
}

After (nanobind):

NB_MODULE(_dgtal, m) {
    nb::class_<Color>(m, "Color")
        .def_property("red",
            static_cast<unsigned char (Color::*)() const>(&Color::red),
            static_cast<void (Color::*)(const unsigned char)>(&Color::red));
}

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
    • Triggering command: /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
    • Triggering command: /usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED ps -c gon.git (dns block)
    • Triggering command: /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)
    • Triggering command: `/usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin REDACTED ps -E .git /home/REDACTED/wor--all e/git-remote-https gtal_nanobind_co/usr/lib/git-core/git rsion

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)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

The current python binding is using pybind11, can you create a PR that use nanobind instead ?


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Jan 2, 2026
Copilot AI and others added 2 commits January 2, 2026 20:22
Co-authored-by: dcoeurjo <700165+dcoeurjo@users.noreply.github.com>
Co-authored-by: dcoeurjo <700165+dcoeurjo@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace pybind11 with nanobind for Python bindings Migrate Python bindings from pybind11 to nanobind Jan 2, 2026
Copilot AI requested a review from dcoeurjo January 2, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants