Skip to content

Commit f6c4c10

Browse files
loriabwjakob
authored andcommitted
restores __invert__ to arithmetic-enabled enum, fixes #1907 (#1909)
1 parent 5b4751a commit f6c4c10

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

include/pybind11/pybind11.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,8 @@ struct enum_base {
14841484
PYBIND11_ENUM_OP_CONV("__ror__", a | b);
14851485
PYBIND11_ENUM_OP_CONV("__xor__", a ^ b);
14861486
PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b);
1487+
m_base.attr("__invert__") = cpp_function(
1488+
[](object arg) { return ~(int_(arg)); }, is_method(m_base));
14871489
}
14881490
} else {
14891491
PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false);

tests/test_enum.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def test_binary_operators():
140140
assert int(m.Flags.Read | m.Flags.Execute) == 5
141141
assert int(m.Flags.Write | m.Flags.Execute) == 3
142142
assert int(m.Flags.Write | 1) == 3
143+
assert ~m.Flags.Write == -3
143144

144145
state = m.Flags.Read | m.Flags.Write
145146
assert (state & m.Flags.Read) != 0

0 commit comments

Comments
 (0)