Skip to content

feat: numpy scalars #5726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 18, 2025
Merged

Conversation

henryiii
Copy link
Collaborator

@henryiii henryiii commented Jun 13, 2025

Description

Reworked (close #3544) by @sun1638650145. Which is a reworked (close #2060) from @aldanor. Adds wrappers for the scalar values part of the NumPy API. This is a bit more minimal than those, following the current coding style more closely (quite a bit of the diff is just moving stuff around so it's defined before it's used with the new usage), and it supports 32-bit systems (Windows and wasm32).

This PR includes a minor consistency fix:

The string returned by npy_format_descriptor_name<bool> was changed from "bool" to "numpy.bool" to match the naming convention used for other scalar types (e.g., "numpy.int64", "numpy.float32"). This ensures consistent formatting for documentation and diagnostics.

Suggested changelog entry:

  • Support py::numpy_scalar<> / py::make_scalar() for NumPy types.

📚 Documentation preview 📚: https://pybind11--5726.org.readthedocs.build/

@henryiii henryiii marked this pull request as ready for review June 13, 2025 20:28
@henryiii henryiii force-pushed the henryiii/feat/numpyscalars branch from 6870f27 to 9e3752f Compare June 13, 2025 22:47
@henryiii henryiii requested a review from Copilot June 17, 2025 18:07
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements support for NumPy scalars by introducing the py::numpy_scalar type and a corresponding py::make_scalar() helper, along with tests and documentation updates.

  • Introduces new type casters and helper functions (py::make_scalar, numpy_scalar) in the core API.
  • Adds C++ and Python tests to verify proper conversion and behavior of NumPy scalars.
  • Updates documentation to detail usage and limitations of the new NumPy scalar support.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_numpy_scalars.py Adds tests for NumPy scalar type conversions using the new py::numpy_scalar API
tests/test_numpy_scalars.cpp Registers and tests scalar operations using lambdas with py::numpy_scalar and py::make_scalar
tests/CMakeLists.txt Adds the new test executable to the test target list
include/pybind11/numpy.h Adds forward declarations, type caster, numpy_scalar struct, and helper functions
docs/advanced/pycpp/numpy.rst Updates documentation with a new section on scalar types and their usage in pybind11

@henryiii henryiii force-pushed the henryiii/feat/numpyscalars branch from fdf2def to 8379d25 Compare June 17, 2025 22:56
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

@henryiii The production code is great, but I have a bunch of suggestions for the test. I'd be happy to work on that directly, these are all very simple things, please let me know.

@henryiii
Copy link
Collaborator Author

henryiii commented Jun 18, 2025

If you'd like to, go ahead. I won't be able to work on it for a few hours. I'll make sure you have access to my fork so you can push directly if you need to.

Edit: I guess you actually have access to any PR branch, but just in case.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

@henryiii All done. Could you please review my changes?

@henryiii henryiii merged commit cf3d1a7 into pybind:master Jun 18, 2025
82 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jun 18, 2025
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Jul 10, 2025
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