Skip to content

feat(numpy): add public register_numpy_dtype - #6315

Open
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:feat/register-numpy-dtype
Open

henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:feat/register-numpy-dtype

Conversation

@henryiii

@henryiii henryiii commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

This is one I might need in boost-histogram; if we provide access via a helper, the stuff we need to register the dtype is not public API. This adds a public API for this missing gap.

🤖 AI text below 🤖

Description

PYBIND11_NUMPY_DTYPE and PYBIND11_NUMPY_DTYPE_EX read the fields of the registered type, so the fields must be accessible members of that type. A type that keeps its data in a private member (for example, boost::histogram::accumulators::mean in boostorg/histogram#323) can only be registered through py::detail::npy_format_descriptor<T>::register_dtype and py::detail::field_descriptor.

This PR adds a public interface for that:

  • py::register_numpy_dtype<T>(fields) forwards to the existing register_dtype.
  • py::numpy_field_descriptor is an alias for detail::field_descriptor.
static_assert(sizeof(Wrapper) == sizeof(Data), "layouts must match");
py::register_numpy_dtype<Wrapper>({
    PYBIND11_FIELD_DESCRIPTOR_EX(Data, n_, "count"),
    PYBIND11_FIELD_DESCRIPTOR(Data, v_),
});

pybind11 does not check the field offsets against the registered type. The docs say this.

The docs now also describe PYBIND11_NUMPY_DTYPE_EX, PYBIND11_FIELD_DESCRIPTOR, and PYBIND11_FIELD_DESCRIPTOR_EX, which were not documented.

Suggested changelog entry:

  • Added py::register_numpy_dtype<T>() and py::numpy_field_descriptor to register a structured NumPy dtype from a list of field descriptors, including fields from a layout-compatible type.

Add py::register_numpy_dtype<T>() and py::numpy_field_descriptor so a
structured dtype can be registered from a list of field descriptors
without py::detail. The fields can come from a layout-compatible type,
which is necessary when the registered type has private members.

Document PYBIND11_NUMPY_DTYPE_EX and PYBIND11_FIELD_DESCRIPTOR[_EX].

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii
henryiii marked this pull request as ready for review November 26, 2026 05:01
@rwgk

rwgk commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

No actionable findings. The public wrapper uses the same structured-dtype registration path as the macros, including layout and dtype validation. The test registers a wrapper with private members by supplying the field descriptors explicitly, then checks the renamed fields and the C++/Python round trip. All 21 focused NumPy dtype tests passed on regular and free-threaded CPython 3.14.10. Wrapper, Regression

Generated by Codex. Ralf has yet to review these findings.

This branch has not been deployed

No deployments
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