-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
feat: numpy scalars #5726
Conversation
6870f27
to
9e3752f
Compare
There was a problem hiding this 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 |
Co-authored-by: Steve R. Sun <1638650145@qq.com> Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
fdf2def
to
8379d25
Compare
There was a problem hiding this 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.
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. |
There was a problem hiding this 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?
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:
py::numpy_scalar<>
/py::make_scalar()
for NumPy types.📚 Documentation preview 📚: https://pybind11--5726.org.readthedocs.build/