Skip to content

Port from pybind11 to nanobind#34

Open
wdeconinck wants to merge 4 commits into
fix-editable-buildsfrom
nanobind
Open

Port from pybind11 to nanobind#34
wdeconinck wants to merge 4 commits into
fix-editable-buildsfrom
nanobind

Conversation

@wdeconinck

Copy link
Copy Markdown
Collaborator

This PR ports the pybind11 implementation to use nanobind instead.
Unit tests had already been improved with previous PR #32, to catch errors in porting, and are untouched here.

Especially noteworthy here are the new array-view interfaces which nanobind brings more naturally, with support for DLPACK etc.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Ports the C++/Python bindings from pybind11 to nanobind, updating build configuration and exposing nanobind-backed array/ndarray interfaces (incl. array API / DLPack-related hooks).

Changes:

  • Switch CMake build from pybind11_add_module to nanobind_add_module and update dependency discovery.
  • Replace pybind11 binding code with nanobind equivalents across all exposed Atlas types.
  • Introduce nanobind ndarray-based views for Field (host/device access + NumPy/CuPy/DLPack entry points).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/atlas4py/CMakeLists.txt Updates build to find/link nanobind and build the extension with nanobind_add_module.
src/atlas4py/_atlas4py.cpp Rewrites the binding layer for nanobind and adds ndarray/array interop for fields.
pyproject.toml Switches build-system requirement from pybind11 to nanobind.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/atlas4py/_atlas4py.cpp Outdated
Comment thread src/atlas4py/_atlas4py.cpp
Comment thread src/atlas4py/_atlas4py.cpp
Comment thread src/atlas4py/_atlas4py.cpp
Comment thread src/atlas4py/_atlas4py.cpp Outdated
Comment thread src/atlas4py/_atlas4py.cpp
@wdeconinck wdeconinck marked this pull request as draft April 10, 2026 15:56
@wdeconinck wdeconinck marked this pull request as ready for review April 29, 2026 20:40
@wdeconinck

Copy link
Copy Markdown
Collaborator Author

Hi @tehrengruber this should now be ready for you to review. Thanks!

@wdeconinck wdeconinck changed the base branch from master to fix-editable-builds May 4, 2026 14:38
@wdeconinck wdeconinck force-pushed the nanobind branch 2 times, most recently from 1f4b632 to 503f6d4 Compare May 4, 2026 15:41

@tehrengruber tehrengruber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, only minor things.

Comment thread src/atlas4py/_atlas4py.cpp Outdated
case DataType::KIND_UINT64:
return "uint64";
default:
return "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the default case is a bug, not wrong user input

Suggested change
return "";
assert(False && "Unexpected DataType")
std::abort();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added

throw nb::type_error("Unexpected DataType");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning was this is an impossible case so I didn't want to make this except and allow the compiler to deduce this case not reachable. Probably overkill for a dtype deduction...

Comment thread src/atlas4py/_atlas4py.cpp Outdated
Comment thread src/atlas4py/_atlas4py.cpp Outdated
Comment thread src/atlas4py/_atlas4py.cpp
}, nb::rv_policy::reference_internal)
// Numpy array interface, see https://numpy.org/doc/stable/reference/arrays.interface.html
.def("__array__", [](Field &self, nb::handle dtype, nb::handle copy) {
return make_ndarray<nb::numpy>(self, MemorySpace::host);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An assert that dtype and copy is indeed None is missing, otherwise this is silently assumed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be handled in 4c4ee65 including a test ; my comment got lost.

Comment thread src/atlas4py/_atlas4py.cpp
Comment thread src/atlas4py/_atlas4py.cpp
@wdeconinck

Copy link
Copy Markdown
Collaborator Author

Thanks @tehrengruber for your excellent review. I believe I have addressed all suggestions now.
It is great that we can use nanobind after this.

Keep in mind this particular PR is stacked on #36 (Fix editable installs), which is not yet reviewed.

@tehrengruber

Copy link
Copy Markdown
Contributor

This is not addressed right?: #34 (comment)

Keep in mind this particular PR is stacked on #36 (Fix editable installs), which is not yet reviewed.

Ah right, I missed this since the PR number is higher. I put some comments there.

Thanks for the work!

@wdeconinck

wdeconinck commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

I did address the comment but somehow my response got lost 😮. I added treatment for this, including a test that verifies if incompatible arguments are passed.

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.

3 participants