Skip to content

Commit cd74b30

Browse files
Xiaofei Wangcopybara-github
authored andcommitted
Do not throw value errors when integers are not a valid value of the proto enum.
PiperOrigin-RevId: 549473332
1 parent 5baa2dc commit cd74b30

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

pybind11_protobuf/enum_type_caster.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ struct enum_type_caster {
5959
base_caster base;
6060
if (base.load(src, convert)) {
6161
T v = static_cast<T>(base);
62-
if (::google::protobuf::GetEnumDescriptor<EnumType>()->FindValueByNumber(v) ==
63-
nullptr) {
64-
throw pybind11::value_error("Invalid value " + std::to_string(v) +
65-
" for ::google::protobuf::Enum " +
66-
pybind11::type_id<EnumType>());
67-
}
62+
// Behavior change 2023-07-19: Previously we only accept integers that
63+
// are valid values of the enum.
6864
value = static_cast<EnumType>(v);
6965
return true;
7066
}

pybind11_protobuf/tests/proto_enum_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def test_enum(self):
1919
self.assertEqual(
2020
m.adjust_enum(test_pb2.TestMessage.ONE), test_pb2.TestMessage.TWO)
2121
self.assertEqual(m.adjust_enum(m.ZERO), m.ONE)
22-
with self.assertRaises(ValueError):
23-
m.adjust_enum(7)
22+
self.assertEqual(m.adjust_enum(7), m.ZERO)
2423
with self.assertRaises(TypeError):
2524
m.adjust_enum('ZERO')
2625

0 commit comments

Comments
 (0)