fix: accept __index__ objects for unsigned integers without conversion so enum == numpy int works (#6193)
* fix: accept __index__ objects for unsigned integers without conversion
Since #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 #6192.
* style: brace the early return now compiled on CPython
The no-convert early return was previously PyPy-only, so clang-tidy never
saw it; readability-braces-around-statements now flags it.
* test: cover the unsigned integer caster directly
Add uint_passthrough(_noconvert) and run the existing int conversion
matrix against it, which also pins __index__ taking precedence over
__int__ for unsigned targets. Gate the explicit PyNumber_Index call on
PYBIND11_INDEX_CHECK so objects without __index__ no longer raise and
clear an extra TypeError in convert mode, and select the PyPy/unsigned
condition with a constexpr bool instead of two #if'd if-heads.
Assisted-by: ClaudeCode:claude-fable-5-1
* test: only expect the 3.9 __int__ DeprecationWarning for signed ints
PyLong_AsUnsignedLong does not call __int__, so the unsigned caster
falls back to PyNumber_Long and does not warn.
Assisted-by: ClaudeCode:claude-opus-5-5
* refactor: drop the PyPy-only __index__ branch in the integer caster
PyPy below the supported 7.3.17 is not a target, and supported PyPy calls __index__ in PyLong_AsLong[Long] like CPython, so only unsigned targets need the explicit PyNumber_Index call. Also drops the complex.h comment that pointed at the removed branch.
Assisted-by: ClaudeCode:claude-opus-5-5
* fix: preserve complex conversion precedence on PyPy
Keep complex instances and __complex__ methods ahead of the legacy __index__ fallback. Retain support for older PyPy versions and update the workaround comment.
Add regression coverage for mixed protocols, complex subclasses, and failing __complex__ methods.
Assisted-by: Codex
* Make PyPy PyComplex_AsCComplex workaround specific to PyPy < 8.0
---------
Co-authored-by: Henry Schreiner <henryfs@princeton.edu>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
6 files changed