Skip to content

Commit ae07bd2

Browse files
committed
Advertise Enum, IntEnum, Flag, IntFlags are compatible stdlib enum types in the documentation (as suggested by gh-timohl, #5555 (review)); add test for enum.Flag to ensure that is actually true.
1 parent cbb7494 commit ae07bd2

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

docs/classes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ in these ways:
563563

564564
underlying = red.value
565565

566+
As of Python 3.13, the compatible `types in the stdlib enum module
567+
<https://docs.python.org/3/library/enum.html#module-contents>`_ are:
568+
``Enum``, ``IntEnum``, ``Flag``, ``IntFlag``.
569+
566570
.. note::
567571

568572
In rare cases, a C++ enum may be bound to Python via a

tests/test_native_enum.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ enum class altitude : char {
2020
low = 'l', // trailing comma only allowed after CWG518
2121
};
2222

23-
enum class combinable { trait1 = 0x1, trait2 = 0x2, trait3 = 0x4 };
23+
enum class flags_uchar : unsigned char { bit0 = 0x1u, bit1 = 0x2u, bit2 = 0x4u };
24+
enum class flags_uint : unsigned int { bit0 = 0x1u, bit1 = 0x2u, bit2 = 0x4u };
2425

2526
enum class export_values { exv0, exv1 };
2627

@@ -91,10 +92,16 @@ TEST_SUBMODULE(native_enum, m) {
9192
.value("low", altitude::low)
9293
.finalize();
9394

94-
py::native_enum<combinable>(m, "combinable", "enum.IntFlag")
95-
.value("trait1", combinable::trait1)
96-
.value("trait2", combinable::trait2)
97-
.value("trait3", combinable::trait3)
95+
py::native_enum<flags_uchar>(m, "flags_uchar", "enum.Flag")
96+
.value("bit0", flags_uchar::bit0)
97+
.value("bit1", flags_uchar::bit1)
98+
.value("bit2", flags_uchar::bit2)
99+
.finalize();
100+
101+
py::native_enum<flags_uint>(m, "flags_uint", "enum.IntFlag")
102+
.value("bit0", flags_uint::bit0)
103+
.value("bit1", flags_uint::bit1)
104+
.value("bit2", flags_uint::bit2)
98105
.finalize();
99106

100107
py::native_enum<export_values>(m, "export_values", "enum.IntEnum")

tests/test_native_enum.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
("low", "l"),
2727
)
2828

29-
COMBINABLE_MEMBERS = (
30-
("trait1", 0x1),
31-
("trait2", 0x2),
32-
("trait3", 0x4),
29+
FLAGS_UCHAR_MEMBERS = (
30+
("bit0", 0x1),
31+
("bit1", 0x2),
32+
("bit2", 0x4),
33+
)
34+
35+
FLAGS_UINT_MEMBERS = (
36+
("bit0", 0x1),
37+
("bit1", 0x2),
38+
("bit2", 0x4),
3339
)
3440

3541
CLASS_WITH_ENUM_IN_CLASS_MEMBERS = (
@@ -52,7 +58,8 @@
5258
(m.smallenum, SMALLENUM_MEMBERS),
5359
(m.color, COLOR_MEMBERS),
5460
(m.altitude, ALTITUDE_MEMBERS),
55-
(m.combinable, COMBINABLE_MEMBERS),
61+
(m.flags_uchar, FLAGS_UCHAR_MEMBERS),
62+
(m.flags_uint, FLAGS_UINT_MEMBERS),
5663
(m.export_values, EXPORT_VALUES_MEMBERS),
5764
(m.member_doc, MEMBER_DOC_MEMBERS),
5865
(m.class_with_enum.in_class, CLASS_WITH_ENUM_IN_CLASS_MEMBERS),
@@ -88,11 +95,12 @@ def test_pickle_roundtrip(enum_type, members):
8895
assert restored == orig
8996

9097

91-
def test_enum_intflag():
92-
traits13 = m.combinable.trait1 | m.combinable.trait3
93-
assert m.combinable.trait1 in traits13
94-
assert m.combinable.trait2 not in traits13
95-
assert m.combinable.trait3 in traits13
98+
@pytest.mark.parametrize("enum_type", [m.flags_uchar, m.flags_uint])
99+
def test_enum_flag(enum_type):
100+
bits02 = enum_type.bit0 | enum_type.bit2
101+
assert enum_type.bit0 in bits02
102+
assert enum_type.bit1 not in bits02
103+
assert enum_type.bit2 in bits02
96104

97105

98106
def test_export_values():

0 commit comments

Comments
 (0)