Repository navigation
fix: accept __index__ objects for unsigned integers without conversion so enum == numpy int works - #6193
Open
quinncheong wants to merge 2 commits into
Open
fix: accept __index__ objects for unsigned integers without conversion so enum == numpy int works#6193quinncheong wants to merge 2 commits into
quinncheong wants to merge 2 commits into
Conversation
Since pybind#5887 an enum with an unsigned underlying type compares unequal to numpy integers, because the no-convert pass rejects __index__ objects for unsigned targets and the catch-all overload returns false. Closes pybind#6192.
The no-convert early return was previously PyPy-only, so clang-tidy never saw it; readability-braces-around-statements now flags it.
Author
|
Pushed 2fa7d31 for the Clang-Tidy failure ( The other red jobs on the first run were not test failures: 21 were cancelled with "The job was not acquired by Runner of type hosted even after multiple attempts", and one Windows job lost contact with its runner. The test jobs that did run passed, across ubuntu, macos and windows at C++11 through C++23, including the free-threaded builds. Prepared with an AI coding agent (Claude Code), checked by me. |
This was referenced Oct 6, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #6192. Related: #5895.
Since #5887,
py::enum_comparison against a scalar uses typed overloads:(Type, Scalar)and then a catch-all(Type, const object &)that returnsfalse. For an enum with an unsigned underlying type, the integer caster rejects__index__objects (numpy integers among them) in the no-convert pass, becausePyLong_AsUnsignedLong[Long]does not call__index__. The catch-all then matches in the same pass, somember == np.int32(3)isFalsewhilenp.int32(3) == memberisTrue. Signed underlying types are unaffected becausePyLong_AsLongdoes call__index__.The caster already calls
PyNumber_Indexexplicitly on PyPy for the same reason. This change runs that path on CPython too when the target type is unsigned. The new test fails on master for theuint32_tenum and passes with the fix; theint32_tenum is the control.Testing
Built
pybind11_testsby hand fromtests/pybind11_tests.cpp,tests/test_enum.cppandtests/test_builtin_casters.cpp(g++ 11.5,-std=c++17, CPython 3.11, numpy 2.4) and ranpytest test_enum.py test_builtin_casters.py: on master the new test fails for theuint32_tenum (1 failed, 37 passed); with this change all 38 pass. I have not run the full suite,prek -a, PyPy, GraalPy or MSVC locally, so I am relying on CI for those.Suggested changelog entry:
__index__objects when the enum's underlying type is unsigned (regression in 3.1.0); unsigned integer arguments now accept__index__objects without implicit conversion, like signed ones.This PR was prepared with an AI coding agent (Claude Code); I reproduced the bug and ran the tests listed above before posting.