Skip to content

Commit 31680e6

Browse files
committed
Implicit conversion from enum to int for Python 3.8 (fix by @sizmailov)
1 parent 5fd187e commit 31680e6

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

include/pybind11/pybind11.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,10 @@ template <typename Type> class enum_ : public class_<Type> {
15661566
#if PY_MAJOR_VERSION < 3
15671567
def("__long__", [](Type value) { return (Scalar) value; });
15681568
#endif
1569+
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8
1570+
def("__index__", [](Type value) { return (Scalar) value; });
1571+
#endif
1572+
15691573
cpp_function setstate(
15701574
[](Type &value, Scalar arg) { value = static_cast<Type>(arg); },
15711575
is_method(*this));

tests/test_enum.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,12 @@ def test_binary_operators():
192192

193193

194194
def test_enum_to_int():
195-
import sys
196-
# Implicit conversion to integers is deprecated in Python >= 3.8
197-
if sys.version_info < (3, 8):
198-
m.test_enum_to_int(m.Flags.Read)
199-
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
200-
m.test_enum_to_uint(m.Flags.Read)
201-
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
202-
m.test_enum_to_long_long(m.Flags.Read)
203-
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
195+
m.test_enum_to_int(m.Flags.Read)
196+
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
197+
m.test_enum_to_uint(m.Flags.Read)
198+
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
199+
m.test_enum_to_long_long(m.Flags.Read)
200+
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
204201

205202

206203
def test_duplicate_enum_name():

0 commit comments

Comments
 (0)