Skip to content

[BUG]: enum __eq__ returns False against numpy integers and other __index__ objects when the enum's underlying type is unsigned (regression in 3.1.0) #6192

Closed

Description

@quinncheong

Required prerequisites

  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • Consider asking first in the Gitter chat room or in a Discussion.

What version (or hash if on master) of pybind11 are you using?

3.1.0; also master a968c27

Problem description

What happens. For a py::enum_ over an unscoped enum whose underlying type is unsigned, member == np.int32(3) returns False and member != np.int32(3) returns True even though int(member) == 3. The reflected comparison np.int32(3) == member returns True, so equality is no longer symmetric. Plain int works. The same enum with a signed underlying type compares equal. Unscoped C enums with only non-negative enumerators get an unsigned underlying type on GCC and Clang, so this hits ordinary C APIs: with MuJoCo 3.14.0 (built against pybind11 3.1.0), mujoco.mjtJoint.mjJNT_HINGE == model.jnt_type[0] is False because jnt_type is an int32 numpy array; it is True with MuJoCo 3.11.0. Downstream code that asserts on such comparisons (robosuite, for example) breaks.

Why. #5887 (in 3.1.0) replaced the generic int_(a).equal(b) comparison with typed overloads. For a convertible enum, __eq__ is now three overloads, tried in order (include/pybind11/pybind11.h on master a968c27, lines 2086-2090 and 2112):

  1. (Type, Type *),
  2. (Type, Scalar) via PYBIND11_ENUM_OP_SCALAR,
  3. (Type, const object &) via PYBIND11_ENUM_OP_REJECT_UNRELATED_TYPE, which returns false.

Overload dispatch first tries every overload without implicit conversion. In that pass the integer caster for an unsigned Scalar lets an __index__ object through the PYBIND11_INDEX_CHECK gate (include/pybind11/cast.h lines 252-254), but then calls as_unsigned (cast.h line 279, include/pybind11/pytypes.h lines 1695-1703), which is PyLong_AsUnsignedLong[Long]. Unlike PyLong_AsLong, those functions do not call __index__; they raise TypeError for anything that is not an int. The explicit PyNumber_Index call that would handle this exists (cast.h lines 262-277) but is compiled only under PYPY_VERSION. So overload 2 fails in the no-convert pass, overload 3 accepts any object in the same pass, and __eq__ returns False before the convert pass (which would have succeeded via PyNumber_Long) is reached. Signed scalars pass in the first pass because PyLong_AsLong calls __index__. Before #5887, int_(a).equal(b) delegated to Python and the numpy scalar's reflected __eq__ produced True. The caster asymmetry itself is described in #5895; #5887 turned it into a wrong comparison result.

Repro. Below; no dependencies besides pybind11 for the __index__ case, numpy for the numpy case. Built with g++ -O1 -shared -fPIC -std=c++17 -I<pybind11>/include $(python3-config --includes) repro_ext.cpp -o repro_ext$(python3-config --extension-suffix).

Output against master a968c27 (identical on v3.1.0):

Signed   a == 3: True  a == np.int32(3): True  np.int32(3) == a: True  a == Idx(): True  a != Idx(): False
USigned  a == 3: True  a == np.int32(3): False  np.int32(3) == a: True  a == Idx(): False  a != Idx(): True

Against v3.0.4:

Signed   a == 3: True  a == np.int32(3): True  np.int32(3) == a: True  a == Idx(): False  a != Idx(): True
USigned  a == 3: True  a == np.int32(3): True  np.int32(3) == a: True  a == Idx(): False  a != Idx(): True

(In 3.0.4 a bare __index__ object compared unequal for both kinds; since 3.1.0 the signed enum compares it equal and the unsigned one does not.)

The same thing through MuJoCo wheels, pip install mujoco numpy:

import numpy as np, mujoco
h = mujoco.mjtJoint.mjJNT_HINGE
print(h == np.int32(3), np.int32(3) == h)

mujoco 3.14.0 prints False True; mujoco 3.11.0 prints True True.

Suggested fix. In the integer caster, call PyNumber_Index for non-int sources when the target is unsigned, as the PyPy branch already does for all integer targets. That makes the no-convert pass accept __index__ objects for unsigned types exactly as it already does for signed types, so overload 2 matches and the comparison is correct. It also resolves the "unsigned noconvert only accepts PyLong" part of #5895. A PR with the patch and a regression test follows and links here. One visible side effect: an unsigned argument that has both __int__ and __index__ now converts through __index__, which is what signed arguments already do.

Environment. Amazon Linux 2023, x86_64; CPython 3.11.14; g++ 11.5.0; numpy 2.4.6; pybind11 v3.0.4, v3.1.0 and master a968c27; mujoco 3.11.0 and 3.14.0 wheels from PyPI.

Reproducible example code

// repro_ext.cpp
#include <pybind11/pybind11.h>
#include <cstdint>
namespace py = pybind11;
enum USigned : std::uint32_t { UA = 3 };
enum Signed : std::int32_t { SA = 3 };
PYBIND11_MODULE(repro_ext, m) {
    py::enum_<USigned>(m, "USigned").value("A", UA);
    py::enum_<Signed>(m, "Signed").value("A", SA);
}

# repro.py
import numpy as np, repro_ext as r
class Idx:
    def __index__(self): return 3
for E in (r.Signed, r.USigned):
    a = E.A
    print(f"{E.__name__:8} a == 3: {a == 3}  a == np.int32(3): {a == np.int32(3)}  "
          f"np.int32(3) == a: {np.int32(3) == a}  a == Idx(): {a == Idx()}  a != Idx(): {a != Idx()}")

Is this a regression? Put the last known working version here if it is.

3.0.4


This report was prepared with an AI coding agent (Claude Code) and reproduced before posting by me.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    f1f34ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions