diff --git a/.github/workflows/bazel_tests.yml b/.github/workflows/bazel_tests.yml index b815a54dd3..44c56f31cc 100644 --- a/.github/workflows/bazel_tests.yml +++ b/.github/workflows/bazel_tests.yml @@ -62,5 +62,7 @@ jobs: - name: Install dependencies run: sudo apt update && sudo apt install -y ${{ matrix.install }} if: matrix.install != '' + - name: Install numpy + run: pip install numpy - name: Run tests run: cd ${{ github.workspace }} && PATH=/tmp/venv/bin:$PATH CC=${{ matrix.CC }} ${{ matrix.BAZEL }} test --test_output=errors $BAZEL_CACHE $BAZEL_CACHE_AUTH ... ${{ matrix.flags }} diff --git a/python/pb_unit_tests/BUILD b/python/pb_unit_tests/BUILD index 81f3cbcb4b..d6be9b0ba7 100644 --- a/python/pb_unit_tests/BUILD +++ b/python/pb_unit_tests/BUILD @@ -24,6 +24,9 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. load(":pyproto_test_wrapper.bzl", "pyproto_test_wrapper") +# begin:github_only +load("@pip_deps//:requirements.bzl", "requirement") +# end:github_only licenses(["notice"]) @@ -45,6 +48,13 @@ pyproto_test_wrapper(name = "message_factory_test") pyproto_test_wrapper(name = "message_test") +# begin:github_only +pyproto_test_wrapper( + name = "numpy_test", + deps = [requirement("numpy")], +) +# end:github_only + # begin:google_only # pyproto_test_wrapper(name = "numpy_test") # end:google_only diff --git a/python/pb_unit_tests/numpy_test_wrapper.py b/python/pb_unit_tests/numpy_test_wrapper.py index 37d28c5b03..10fd149398 100644 --- a/python/pb_unit_tests/numpy_test_wrapper.py +++ b/python/pb_unit_tests/numpy_test_wrapper.py @@ -25,190 +25,7 @@ import unittest -# begin:google_only -# from google.protobuf.internal.numpy_test import * -# end:google_only - -# begin:github_only -# TODO(b/240447513) Delete workaround after numpy_test is open-sourced in -# protobuf github. -import unittest - -import numpy as np - -from google.protobuf import unittest_pb2 -from google.protobuf.internal import testing_refleaks - -message = unittest_pb2.TestAllTypes() -np_float_scalar = np.float64(0.0) -np_1_float_array = np.zeros(shape=(1,), dtype=np.float64) -np_2_float_array = np.zeros(shape=(2,), dtype=np.float64) -np_11_float_array = np.zeros(shape=(1, 1), dtype=np.float64) -np_22_float_array = np.zeros(shape=(2, 2), dtype=np.float64) - -np_int_scalar = np.int64(0) -np_1_int_array = np.zeros(shape=(1,), dtype=np.int64) -np_2_int_array = np.zeros(shape=(2,), dtype=np.int64) -np_11_int_array = np.zeros(shape=(1, 1), dtype=np.int64) -np_22_int_array = np.zeros(shape=(2, 2), dtype=np.int64) - -np_uint_scalar = np.uint64(0) -np_1_uint_array = np.zeros(shape=(1,), dtype=np.uint64) -np_2_uint_array = np.zeros(shape=(2,), dtype=np.uint64) -np_11_uint_array = np.zeros(shape=(1, 1), dtype=np.uint64) -np_22_uint_array = np.zeros(shape=(2, 2), dtype=np.uint64) - -np_bool_scalar = np.bool_(False) -np_1_bool_array = np.zeros(shape=(1,), dtype=np.bool_) -np_2_bool_array = np.zeros(shape=(2,), dtype=np.bool_) -np_11_bool_array = np.zeros(shape=(1, 1), dtype=np.bool_) -np_22_bool_array = np.zeros(shape=(2, 2), dtype=np.bool_) - -@testing_refleaks.TestCase -class NumpyIntProtoTest(unittest.TestCase): - - # Assigning dim 1 ndarray of ints to repeated field should pass - def testNumpyDim1IntArrayToRepeated_IsValid(self): - message.repeated_int64[:] = np_1_int_array - message.repeated_int64[:] = np_2_int_array - - message.repeated_uint64[:] = np_1_uint_array - message.repeated_uint64[:] = np_2_uint_array - - # Assigning dim 2 ndarray of ints to repeated field should fail - def testNumpyDim2IntArrayToRepeated_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.repeated_int64[:] = np_11_int_array - with self.assertRaises(TypeError): - message.repeated_int64[:] = np_22_int_array - - with self.assertRaises(TypeError): - message.repeated_uint64[:] = np_11_uint_array - with self.assertRaises(TypeError): - message.repeated_uint64[:] = np_22_uint_array - - # Assigning any ndarray of floats to repeated int field should fail - def testNumpyFloatArrayToRepeated_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.repeated_int64[:] = np_1_float_array - with self.assertRaises(TypeError): - message.repeated_int64[:] = np_11_float_array - with self.assertRaises(TypeError): - message.repeated_int64[:] = np_22_float_array - - # Assigning any np int to scalar field should pass - def testNumpyIntScalarToScalar_IsValid(self): - message.optional_int64 = np_int_scalar - message.optional_uint64 = np_uint_scalar - - # Assigning any ndarray of ints to scalar field should fail - def testNumpyIntArrayToScalar_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.optional_int64 = np_1_int_array - with self.assertRaises(TypeError): - message.optional_int64 = np_11_int_array - with self.assertRaises(TypeError): - message.optional_int64 = np_22_int_array - - with self.assertRaises(TypeError): - message.optional_uint64 = np_1_uint_array - with self.assertRaises(TypeError): - message.optional_uint64 = np_11_uint_array - with self.assertRaises(TypeError): - message.optional_uint64 = np_22_uint_array - - # Assigning any ndarray of floats to scalar field should fail - def testNumpyFloatArrayToScalar_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.optional_int64 = np_1_float_array - with self.assertRaises(TypeError): - message.optional_int64 = np_11_float_array - with self.assertRaises(TypeError): - message.optional_int64 = np_22_float_array - -@testing_refleaks.TestCase -class NumpyFloatProtoTest(unittest.TestCase): - - # Assigning dim 1 ndarray of floats to repeated field should pass - def testNumpyDim1FloatArrayToRepeated_IsValid(self): - message.repeated_float[:] = np_1_float_array - message.repeated_float[:] = np_2_float_array - - # Assigning dim 2 ndarray of floats to repeated field should fail - def testNumpyDim2FloatArrayToRepeated_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.repeated_float[:] = np_11_float_array - with self.assertRaises(TypeError): - message.repeated_float[:] = np_22_float_array - - # Assigning any np float to scalar field should pass - def testNumpyFloatScalarToScalar_IsValid(self): - message.optional_float = np_float_scalar - - # Assigning any ndarray of float to scalar field should fail - def testNumpyFloatArrayToScalar_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.optional_float = np_1_float_array - with self.assertRaises(TypeError): - message.optional_float = np_11_float_array - with self.assertRaises(TypeError): - message.optional_float = np_22_float_array - -@testing_refleaks.TestCase -class NumpyBoolProtoTest(unittest.TestCase): - - # Assigning dim 1 ndarray of bool to repeated field should pass - def testNumpyDim1BoolArrayToRepeated_IsValid(self): - message.repeated_bool[:] = np_1_bool_array - message.repeated_bool[:] = np_2_bool_array - - # Assigning dim 2 ndarray of bool to repeated field should fail - def testNumpyDim2BoolArrayToRepeated_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.repeated_bool[:] = np_11_bool_array - with self.assertRaises(TypeError): - message.repeated_bool[:] = np_22_bool_array - - # Assigning any np bool to scalar field should pass - def testNumpyBoolScalarToScalar_IsValid(self): - message.optional_bool = np_bool_scalar - - # Assigning any ndarray of bool to scalar field should fail - def testNumpyBoolArrayToScalar_RaisesTypeError(self): - with self.assertRaises(TypeError): - message.optional_bool = np_1_bool_array - with self.assertRaises(TypeError): - message.optional_bool = np_11_bool_array - with self.assertRaises(TypeError): - message.optional_bool = np_22_bool_array - -@testing_refleaks.TestCase -class NumpyProtoIndexingTest(unittest.TestCase): - - def testNumpyIntScalarIndexing_Passes(self): - data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2]) - self.assertEqual(0, data.repeated_int64[np.int64(0)]) - - def testNumpyNegative1IntScalarIndexing_Passes(self): - data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2]) - self.assertEqual(2, data.repeated_int64[np.int64(-1)]) - - def testNumpyFloatScalarIndexing_Fails(self): - data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2]) - with self.assertRaises(TypeError): - _ = data.repeated_int64[np.float64(0.0)] - - def testNumpyIntArrayIndexing_Fails(self): - data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2]) - with self.assertRaises(TypeError): - _ = data.repeated_int64[np.array([0])] - with self.assertRaises(TypeError): - _ = data.repeated_int64[np.ndarray((1,), buffer=np.array([0]), dtype=int)] - with self.assertRaises(TypeError): - _ = data.repeated_int64[np.ndarray((1, 1), - buffer=np.array([0]), - dtype=int)] -# end:github_only +from google.protobuf.internal.numpy_test import * if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/python/pb_unit_tests/pyproto_test_wrapper.bzl b/python/pb_unit_tests/pyproto_test_wrapper.bzl index 50bd9212ae..8ea38789c0 100644 --- a/python/pb_unit_tests/pyproto_test_wrapper.bzl +++ b/python/pb_unit_tests/pyproto_test_wrapper.bzl @@ -1,6 +1,6 @@ # begin:github_only -def pyproto_test_wrapper(name): +def pyproto_test_wrapper(name, deps = []): src = name + "_wrapper.py" native.py_test( name = name, @@ -14,7 +14,7 @@ def pyproto_test_wrapper(name): "@com_google_protobuf//:python_specific_test_protos", "@com_google_protobuf//:python_test_srcs", "@com_google_protobuf//:python_srcs", - ], + ] + deps, ) # end:github_only