diff --git a/pybind11_protobuf/BUILD b/pybind11_protobuf/BUILD index f34fab0..2b7d851 100644 --- a/pybind11_protobuf/BUILD +++ b/pybind11_protobuf/BUILD @@ -71,7 +71,6 @@ cc_library( deps = [ "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", - "@com_google_absl//absl/meta:type_traits", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@com_google_protobuf//:protobuf", diff --git a/pybind11_protobuf/check_unknown_fields.cc b/pybind11_protobuf/check_unknown_fields.cc index 0639d09..0e1e9ea 100644 --- a/pybind11_protobuf/check_unknown_fields.cc +++ b/pybind11_protobuf/check_unknown_fields.cc @@ -6,15 +6,16 @@ #include #include -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" -#include "google/protobuf/unknown_field_set.h" #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_join.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/message.h" +#include "google/protobuf/unknown_field_set.h" +#include "python/google/protobuf/proto_api.h" namespace pybind11_protobuf::check_unknown_fields { namespace { @@ -103,8 +104,7 @@ bool HasUnknownFields::FindUnknownFieldsRecursive( // Stop only if the extension is known by Python. if (py_proto_api->GetDefaultDescriptorPool()->FindExtensionByNumber( - unknown_field_parent_descriptor, - unknown_field_number)) { + unknown_field_parent_descriptor, unknown_field_number)) { field_fqn_parts.resize(depth); return true; } diff --git a/pybind11_protobuf/check_unknown_fields.h b/pybind11_protobuf/check_unknown_fields.h index 79ac001..a448f83 100644 --- a/pybind11_protobuf/check_unknown_fields.h +++ b/pybind11_protobuf/check_unknown_fields.h @@ -2,10 +2,11 @@ #define PYBIND11_PROTOBUF_CHECK_UNKNOWN_FIELDS_H_ #include +#include +#include "absl/strings/string_view.h" #include "google/protobuf/message.h" #include "python/google/protobuf/proto_api.h" -#include "absl/strings/string_view.h" namespace pybind11_protobuf::check_unknown_fields { diff --git a/pybind11_protobuf/enum_type_caster.h b/pybind11_protobuf/enum_type_caster.h index d30369b..d35ea8d 100644 --- a/pybind11_protobuf/enum_type_caster.h +++ b/pybind11_protobuf/enum_type_caster.h @@ -11,21 +11,18 @@ #include #include -#include #include -#include "google/protobuf/descriptor.h" -#include "google/protobuf/generated_enum_reflection.h" #include "google/protobuf/generated_enum_util.h" // pybind11 type_caster specialization which translates Proto::Enum types // to/from ints. This will have ODR conflicts when users specify wrappers for // enums using py::enum_. // -// ::google::protobuf::is_proto_enum and ::google::protobuf::GetEnumDescriptor are require +// ::google::protobuf::is_proto_enum and ::google::protobuf::GetEnumDescriptor are required. // -// NOTE: The protobuf compiler does not generate ::google::protobuf::is_proto_enum traits -// for enumerations of oneof fields. +// NOTE: The protobuf compiler does not generate ::google::protobuf::is_proto_enum +// traits for enumerations of oneof fields. // // Example: // #include diff --git a/pybind11_protobuf/native_proto_caster.h b/pybind11_protobuf/native_proto_caster.h index 6d020e0..806531a 100644 --- a/pybind11_protobuf/native_proto_caster.h +++ b/pybind11_protobuf/native_proto_caster.h @@ -11,22 +11,17 @@ // IWYU #include -#include -#include -#include -#include #include -#include -#include "google/protobuf/message.h" #include "absl/strings/string_view.h" +#include "google/protobuf/message.h" #include "pybind11_protobuf/enum_type_caster.h" #include "pybind11_protobuf/proto_caster_impl.h" -// pybind11::type_caster<> specialization for ::google::protobuf::Message types that -// that converts protocol buffer objects between C++ and python representations. -// This binder supports binaries linked with both native python protos -// and fast cpp python protos. +// pybind11::type_caster<> specialization for ::google::protobuf::Message types +// that that converts protocol buffer objects between C++ and python +// representations. This binder supports binaries linked with both native python +// protos and fast cpp python protos. // // When passing protos between python and C++, if possible, an underlying C++ // object may have ownership transferred, or may be copied if both instances diff --git a/pybind11_protobuf/proto_cast_util.cc b/pybind11_protobuf/proto_cast_util.cc index c2acac9..11a7704 100644 --- a/pybind11_protobuf/proto_cast_util.cc +++ b/pybind11_protobuf/proto_cast_util.cc @@ -11,9 +11,7 @@ #include #include #include -#include -#include "google/protobuf/descriptor.pb.h" #include "absl/container/flat_hash_map.h" #include "absl/log/check.h" #include "absl/log/log.h" @@ -25,6 +23,7 @@ #include "absl/strings/strip.h" #include "absl/types/optional.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor_database.h" #include "google/protobuf/dynamic_message.h" @@ -34,7 +33,6 @@ using ::google::protobuf::Descriptor; using ::google::protobuf::DescriptorDatabase; using ::google::protobuf::DescriptorPool; using ::google::protobuf::DynamicMessageFactory; -using ::google::protobuf::FileDescriptor; using ::google::protobuf::FileDescriptorProto; using ::google::protobuf::Message; using ::google::protobuf::MessageFactory; @@ -184,10 +182,8 @@ GlobalState::GlobalState() { // pybind11_protobuf casting needs a dependency on proto internals to work. try { ImportCached("google.protobuf.descriptor"); - auto descriptor_pool = - ImportCached("google.protobuf.descriptor_pool"); - auto message_factory = - ImportCached("google.protobuf.message_factory"); + auto descriptor_pool = ImportCached("google.protobuf.descriptor_pool"); + auto message_factory = ImportCached("google.protobuf.message_factory"); global_pool_ = descriptor_pool.attr("Default")(); find_message_type_by_name_ = global_pool_.attr("FindMessageTypeByName"); if (hasattr(message_factory, "GetMessageClass")) { diff --git a/pybind11_protobuf/proto_cast_util.h b/pybind11_protobuf/proto_cast_util.h index f2ff440..503f94f 100644 --- a/pybind11_protobuf/proto_cast_util.h +++ b/pybind11_protobuf/proto_cast_util.h @@ -6,16 +6,13 @@ #include #include -#include #include #include -#include -#include -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/message.h" // PYBIND11_PROTOBUF_ASSUME_FULL_ABI_COMPATIBILITY can be defined by users // certain about ABI compatibility between all Python extensions in their diff --git a/pybind11_protobuf/proto_caster_impl.h b/pybind11_protobuf/proto_caster_impl.h index 12e693d..33df74d 100644 --- a/pybind11_protobuf/proto_caster_impl.h +++ b/pybind11_protobuf/proto_caster_impl.h @@ -8,15 +8,13 @@ #include #include -#include #include #include -#include #include #include -#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/message.h" #include "pybind11_protobuf/proto_cast_util.h" @@ -74,8 +72,7 @@ struct proto_caster_load_impl { owned = std::unique_ptr(new ProtoType()); value = owned.get(); - return owned.get()->ParsePartialFromString( - PyBytesAsStringView(serialized_bytes)); + return owned->ParsePartialFromString(PyBytesAsStringView(serialized_bytes)); } // ensure_owned ensures that the owned member contains a copy of the @@ -126,8 +123,7 @@ struct proto_caster_load_impl<::google::protobuf::Message> { src, *descriptor_name) .release())); value = owned.get(); - return owned.get()->ParsePartialFromString( - PyBytesAsStringView(serialized_bytes)); + return owned->ParsePartialFromString(PyBytesAsStringView(serialized_bytes)); } // ensure_owned ensures that the owned member contains a copy of the @@ -156,7 +152,8 @@ struct fast_cpp_cast_impl { (policy == pybind11::return_value_policy::reference || policy == pybind11::return_value_policy::reference_internal)) { throw pybind11::type_error( - "Cannot return a const reference to a ::google::protobuf::Message derived " + "Cannot return a const reference to a ::google::protobuf::Message " + "derived " "type. Consider setting return_value_policy::copy in the " "pybind11 def()."); } diff --git a/pybind11_protobuf/proto_utils.cc b/pybind11_protobuf/proto_utils.cc index 2701727..8e9844b 100644 --- a/pybind11_protobuf/proto_utils.cc +++ b/pybind11_protobuf/proto_utils.cc @@ -7,17 +7,20 @@ #include -#include +#include +#include +#include #include #include #include +#include -#include "google/protobuf/descriptor.pb.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/message.h" #include "google/protobuf/reflection.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" namespace pybind11 { namespace google { @@ -516,7 +519,7 @@ struct FindMapPair { handle key, bool add_key = true) { // When using the proto reflection API, maps are represented as repeated // message fields (messages with 2 elements: 'key' and 'value'). If protocol - // buffers guarrantee a consistent (and useful) ordering of elements, + // buffers guarantee a consistent (and useful) ordering of elements, // it should be possible to do this search in O(log(n)) time. However, that // requires more knowledge of protobuf internals than I have, so for now // assume a random ordering of elements, in which case a O(n) search is diff --git a/pybind11_protobuf/proto_utils.h b/pybind11_protobuf/proto_utils.h index 1b2c084..fa2aeab 100644 --- a/pybind11_protobuf/proto_utils.h +++ b/pybind11_protobuf/proto_utils.h @@ -19,9 +19,9 @@ namespace google { // Unlike ProtoSetField, this allows setting message, map and repeated fields. void ProtoInitFields(::google::protobuf::Message* message, kwargs kwargs_in); -// Wrapper around ::google::protobuf::Message::CopyFrom which can efficiently copy from -// either a wrapped C++ or native python proto. Throws an error if `other` -// is not a proto of the correct type. +// Wrapper around ::google::protobuf::Message::CopyFrom which can efficiently +// copy from either a wrapped C++ or native python proto. Throws an error if +// `other` is not a proto of the correct type. void ProtoCopyFrom(::google::protobuf::Message* msg, handle other); // Allocate and return the ProtoType given by the template argument. diff --git a/pybind11_protobuf/tests/BUILD b/pybind11_protobuf/tests/BUILD index 2cca967..0d68461 100644 --- a/pybind11_protobuf/tests/BUILD +++ b/pybind11_protobuf/tests/BUILD @@ -245,9 +245,9 @@ py_test( srcs_version = "PY3", deps = [ ":test_py_pb2", - "@com_google_protobuf//:protobuf_python", "@com_google_absl_py//absl/testing:absltest", "@com_google_absl_py//absl/testing:parameterized", + "@com_google_protobuf//:protobuf_python", requirement("absl_py"), ], ) @@ -270,6 +270,7 @@ pybind_extension( deps = [ ":test_cc_proto", "//pybind11_protobuf:wrapped_proto_caster", + "@com_google_absl//absl/functional:function_ref", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/types:optional", "@com_google_protobuf//:protobuf", diff --git a/pybind11_protobuf/tests/dynamic_message_module.cc b/pybind11_protobuf/tests/dynamic_message_module.cc index 21edd96..8e56784 100644 --- a/pybind11_protobuf/tests/dynamic_message_module.cc +++ b/pybind11_protobuf/tests/dynamic_message_module.cc @@ -5,12 +5,14 @@ #include -#include +#include +#include #include #include +#include -#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/dynamic_message.h" #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" diff --git a/pybind11_protobuf/tests/dynamic_message_test.py b/pybind11_protobuf/tests/dynamic_message_test.py index d81ce61..0122338 100644 --- a/pybind11_protobuf/tests/dynamic_message_test.py +++ b/pybind11_protobuf/tests/dynamic_message_test.py @@ -10,10 +10,10 @@ from absl.testing import absltest from absl.testing import parameterized - from google.protobuf import descriptor_pb2 from google.protobuf import descriptor_pool from google.protobuf import message_factory + from pybind11_protobuf.tests import compare from pybind11_protobuf.tests import dynamic_message_module as m from pybind11_protobuf.tests import test_pb2 diff --git a/pybind11_protobuf/tests/message_module.cc b/pybind11_protobuf/tests/message_module.cc index 61f5579..b037691 100644 --- a/pybind11_protobuf/tests/message_module.cc +++ b/pybind11_protobuf/tests/message_module.cc @@ -5,9 +5,7 @@ #include -#include -#include -#include +#include #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" @@ -18,8 +16,8 @@ namespace py = ::pybind11; namespace { -using pybind11::test::IntMessage; -using pybind11::test::TestMessage; +using ::pybind11::test::IntMessage; +using ::pybind11::test::TestMessage; PYBIND11_MODULE(message_module, m) { pybind11_protobuf::ImportNativeProtoCasters(); diff --git a/pybind11_protobuf/tests/message_test.py b/pybind11_protobuf/tests/message_test.py index a92caea..9873ebe 100644 --- a/pybind11_protobuf/tests/message_test.py +++ b/pybind11_protobuf/tests/message_test.py @@ -14,12 +14,12 @@ from absl.testing import absltest from absl.testing import parameterized +from google.protobuf import text_format from google.protobuf import any_pb2 from pybind11_protobuf.tests import compare from pybind11_protobuf.tests import message_module as m from pybind11_protobuf.tests import test_pb2 -from google.protobuf import text_format def get_py_message(): diff --git a/pybind11_protobuf/tests/pass_by_module.cc b/pybind11_protobuf/tests/pass_by_module.cc index b2c4ae8..498c0cc 100644 --- a/pybind11_protobuf/tests/pass_by_module.cc +++ b/pybind11_protobuf/tests/pass_by_module.cc @@ -6,13 +6,14 @@ #include #include +#include #include #include -#include +#include #include -#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/dynamic_message.h" #include "google/protobuf/message.h" #include "pybind11_protobuf/native_proto_caster.h" @@ -22,7 +23,7 @@ namespace py = ::pybind11; namespace { -using pybind11::test::IntMessage; +using ::pybind11::test::IntMessage; bool CheckIntMessage(const IntMessage* message, int32_t value) { return message ? message->value() == value : false; diff --git a/pybind11_protobuf/tests/pass_by_test.py b/pybind11_protobuf/tests/pass_by_test.py index 56a69fc..49cc1a6 100644 --- a/pybind11_protobuf/tests/pass_by_test.py +++ b/pybind11_protobuf/tests/pass_by_test.py @@ -9,11 +9,11 @@ from absl.testing import absltest from absl.testing import parameterized +from google.protobuf import descriptor_pool +from google.protobuf import message_factory from pybind11_protobuf.tests import pass_by_module as m from pybind11_protobuf.tests import test_pb2 -from google.protobuf import descriptor_pool -from google.protobuf import message_factory def get_abstract_pass_by_params(): @@ -60,7 +60,7 @@ def get_pass_by_params(): 'concrete_uptr_ref', 'concrete_wref', 'std_variant', - 'std_optional,' + 'std_optional,', ]: try: p.append((x, getattr(m, x))) @@ -115,7 +115,8 @@ def test_pool_proto_check(self, check_method): pool = descriptor_pool.Default() factory = message_factory.MessageFactory(pool) prototype = factory.GetPrototype( - pool.FindMessageTypeByName('pybind11.test.IntMessage')) + pool.FindMessageTypeByName('pybind11.test.IntMessage') + ) message = prototype(value=9) self.assertTrue(check_method(message, 9)) diff --git a/pybind11_protobuf/tests/regression_wrappers_module.cc b/pybind11_protobuf/tests/regression_wrappers_module.cc index 1c246cb..4f63ba3 100644 --- a/pybind11_protobuf/tests/regression_wrappers_module.cc +++ b/pybind11_protobuf/tests/regression_wrappers_module.cc @@ -5,7 +5,9 @@ #include -#include "google/protobuf/descriptor.h" +#include +#include + #include "google/protobuf/message.h" #include "pybind11_protobuf/native_proto_caster.h" diff --git a/pybind11_protobuf/tests/wrapped_proto_module.cc b/pybind11_protobuf/tests/wrapped_proto_module.cc index 3d7f0dc..364054e 100644 --- a/pybind11_protobuf/tests/wrapped_proto_module.cc +++ b/pybind11_protobuf/tests/wrapped_proto_module.cc @@ -6,14 +6,15 @@ #include #include -#include -#include -#include +#include +#include +#include #include -#include "google/protobuf/dynamic_message.h" +#include "absl/functional/function_ref.h" #include "absl/status/statusor.h" #include "absl/types/optional.h" +#include "google/protobuf/dynamic_message.h" #include "pybind11_protobuf/tests/test.pb.h" #include "pybind11_protobuf/wrapped_proto_caster.h" @@ -21,11 +22,11 @@ namespace py = ::pybind11; namespace { -using pybind11::test::IntMessage; -using pybind11::test::TestMessage; -using pybind11_protobuf::WithWrappedProtos; -using pybind11_protobuf::WrappedProto; -using pybind11_protobuf::WrappedProtoKind; +using ::pybind11::test::IntMessage; +using ::pybind11::test::TestMessage; +using ::pybind11_protobuf::WithWrappedProtos; +using ::pybind11_protobuf::WrappedProto; +using ::pybind11_protobuf::WrappedProtoKind; const TestMessage& GetStatic() { static TestMessage test_message = [] { diff --git a/pybind11_protobuf/wrapped_proto_caster.h b/pybind11_protobuf/wrapped_proto_caster.h index 18cf7da..68a6f92 100644 --- a/pybind11_protobuf/wrapped_proto_caster.h +++ b/pybind11_protobuf/wrapped_proto_caster.h @@ -7,21 +7,18 @@ #include #include -#include #include -#include -#include #include #include -#include "google/protobuf/message.h" #include "absl/status/statusor.h" #include "absl/types/optional.h" +#include "google/protobuf/message.h" #include "pybind11_protobuf/proto_cast_util.h" #include "pybind11_protobuf/proto_caster_impl.h" -// pybind11::type_caster<> specialization for ::google::protobuf::Message types using -// pybind11_protobuf::WrappedProto<> wrappers. When passing from C++ to +// pybind11::type_caster<> specialization for ::google::protobuf::Message types +// using pybind11_protobuf::WrappedProto<> wrappers. When passing from C++ to // python a copy is always made. When passing from python to C++ a copy is // made for mutable and by-value types, but not for const reference types. // @@ -68,10 +65,10 @@ inline void ImportWrappedProtoCasters() { InitializePybindProtoCastUtil(); } /// Tag types for WrappedProto specialization. enum WrappedProtoKind : int { kConst, kValue, kMutable }; -/// WrappedProto wraps a ::google::protobuf::Message subtype, exposing implicit -/// constructors and implicit cast operators. The ConstTag variant allows -/// conversion to const; the MutableTag allows mutable conversion and enables -/// the type-caster specialization to enforce that a copy is made. +/// WrappedProto wraps a ::google::protobuf::Message subtype, exposing +/// implicit constructors and implicit cast operators. The ConstTag variant +/// allows conversion to const; the MutableTag allows mutable conversion and +/// enables the type-caster specialization to enforce that a copy is made. /// /// WrappedProto ALWAYS creates a copy. /// WrappedProto ALWAYS creates a copy. @@ -342,8 +339,8 @@ struct WrapHelper>::value>> { static constexpr auto kKind = DetectKind(); static_assert(kKind != WrappedProtoKind::kMutable, - "WithWrappedProtos() does not support mutable ::google::protobuf::Message " - "parameters."); + "WithWrappedProtos() does not support mutable " + "::google::protobuf::Message parameters."); using type = WrappedProto, kKind>; }; @@ -355,8 +352,8 @@ struct WrapHelper, // ::google::protobuf::Message, intrinsic_t>::value>> { static constexpr auto kKind = DetectKind(); static_assert(kKind != WrappedProtoKind::kMutable, - "WithWrappedProtos() does not support mutable ::google::protobuf::Message " - "parameters."); + "WithWrappedProtos() does not support mutable " + "::google::protobuf::Message parameters."); using type = absl::optional, kKind>>; }; @@ -371,8 +368,8 @@ struct WrapHelper, // ::google::protobuf::Message, intrinsic_t>::value>> { static constexpr auto kKind = DetectKind(); static_assert(kKind != WrappedProtoKind::kMutable, - "WithWrappedProtos() does not support mutable ::google::protobuf::Message " - "parameters."); + "WithWrappedProtos() does not support mutable " + "::google::protobuf::Message parameters."); using type = absl::StatusOr, kKind>>; }; @@ -401,8 +398,8 @@ struct WrapHelper>, // }; /// FunctionInvoker/ConstMemberInvoker/MemberInvoker are internal functions -/// that expose calls with equivalent signatures, replacing ::google::protobuf::Message -/// derived types with WrappedProto types. +/// that expose calls with equivalent signatures, replacing +/// ::google::protobuf::Message derived types with WrappedProto types. template struct FunctionInvoker; @@ -464,8 +461,8 @@ struct LambdaSignature { } // namespace impl /// WithWrappedProtos(...) wraps a function type and converts any -/// ::google::protobuf::Message derived types into a WrappedProto<...> of the same -/// underlying proto2 type. +/// ::google::protobuf::Message derived types into a WrappedProto<...> of the +/// same underlying proto2 type. template > auto WithWrappedProtos(F f) ->