Skip to content

[pydrake] Port the common-init module to nanobind - #24783

Open
jwnimmer-tri wants to merge 1 commit into
RobotLocomotion:masterfrom
jwnimmer-tri:nanobind-common
Open

[pydrake] Port the common-init module to nanobind#24783
jwnimmer-tri wants to merge 1 commit into
RobotLocomotion:masterfrom
jwnimmer-tri:nanobind-common

Conversation

@jwnimmer-tri

@jwnimmer-tri jwnimmer-tri commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

All other parts of pydrake depend on this module, so it needs to be the first thing ported.

Towards #21572.


This change is Reviewable

@jwnimmer-tri jwnimmer-tri added status: do not merge priority: medium status: do not review release notes: none This pull request should not be mentioned in the release notes status: defer ci CI will terminate early (saving the expense of unwanted builds) and removed status: do not merge status: do not review status: defer ci CI will terminate early (saving the expense of unwanted builds) labels Jul 24, 2026
@jwnimmer-tri
jwnimmer-tri marked this pull request as ready for review July 24, 2026 22:17

@jwnimmer-tri jwnimmer-tri left a comment

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.

+a:@rpoyner-tri for co-author / co-feature review, please, when time allows.

@jwnimmer-tri reviewed 29 files and all commit messages, and made 4 comments.
Reviewable status: LGTM missing from assignee rpoyner-tri(platform), needs at least two assigned reviewers (waiting on rpoyner-tri).


bindings/pydrake/symbolic_types_pybind.h line 39 at r1 (raw file):

    const uint8_t var_type = static_cast<uint8_t>(hi);
    if (var_type > static_cast<uint8_t>(Variable::Type::RANDOM_EXPONENTIAL)) {
      return std::nullopt;

FYI nanobind requires its type-casters to be noexcept, so we can't provide a more specific error message for invalid arguments like we had with pybind11. Instead, the user will be get a TypeError on the call. The test has been updated to reflect this.


bindings/pydrake/common/BUILD.bazel line 420 at r1 (raw file):

drake_py_unittest(
    name = "eigen_pybind_test",
    # TODO(#21572) Remove this opt-out.

FYI This pull request only ports the essential components of the init module and any helper libraries required by it.

Higher-layer helper modules that are only needed by other modules in pydrake remain as future work, with TODOs on their tests.


bindings/pydrake/common/serialize_pybind.h line 151 at r1 (raw file):

    };
    if constexpr (std::is_same_v<T, bool>) {
      return type_of(py::bool_(false));

FYI The nanobind default constructors for primitives like bool_, int_, float_ etc construct None rather than 0, so we need to pass a non-None value here.

All other parts of pydrake depend on this module, so it needs to be
the first thing ported.

Co-authored-by: Rick Poyner (rico) <rick.poyner@tri.global>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: medium release notes: none This pull request should not be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants